#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node * allocate(){
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
t->next = NULL;
return t;
}
// Insert At Front
struct Node * insertAtFront(struct Node *s){
struct Node *t;
t = allocate();
printf("Enter Data : ");
scanf("%d",&t->data);
t->next = s;
return t;
}
struct Node * insertAtLast(struct Node *s){
struct Node *p , *t;
t = allocate();
printf("Enter Data : ");
scanf("%d",&t->data);
p=s;
while(p->next != NULL){
p = p->next;
}
p->next = t ;
return s;
}
struct Node * insertAtMid(struct Node *s){
struct Node *p,*t;
int i,pos;
t = allocate();
printf("Enter Data : ");
scanf("%d",&t->data);
p=s;
printf("Enter Position : ");
scanf("%d",&pos);
for(i=1 ; i<=pos-2 ; i++){
p = p->next;
}
t->next = p->next ;
p->next = t;
return s;
}
struct Node * deleteFromFront(struct Node *s){
struct Node *t;
t = s;
s = s->next;
t->next = NULL;
free(t);
return s;
}
struct Node * deleteFromLast(struct Node *s){
struct Node *t,*p;
t = s;
while(t->next != NULL ){
p = t;
t = t->next;
}
p->next = NULL;
free(t);
return s;
}
struct Node * deleteFromMid(struct Node *s){
struct Node *t,*p;
int i,pos;
t=s;
printf("Enter Position : ");
scanf("%d",&pos);
for(i=1; i<pos ; i++){
p = t;
t = t->next;
}
p->next = t->next;
t->next = NULL;
free(t);
return s;
}
void traverse(struct Node *s){
while(s != NULL){
printf("%d\t",s->data);
s = s->next;
}
}
void reverseTraverse(struct Node *s){
if(s != NULL){
reverseTraverse(s->next);
printf("%d\t",s->data);
}
}
void nodeCount(struct Node *s){
int count=0;
while(s != NULL){
count++;
s = s->next;
}
printf("Number of Node = %d",count);
}
struct Node * insert(struct Node *s){
int ch,op;
do{
clrscr();
printf("1. Insert At Front");
printf("2. Insert At Mid");
printf("3. Insert At Last");
printf("Enter Choice : ");
scanf("%d",&ch);
switch(ch){
case 1: s = insertAtFront(s);
break;
case 2: s = insertAtMid(s);
break;
case 3: s = insertAtLast(s);
break;
default: printf("Invalid Choice");
}
printf("Press Any Key..");
getch();
clrscr();
printf("Enter Nonzero value for repeat insert module otherwise enter zero : ");
scanf("%d",&op);
}while(op);
return s;
}
struct Node * deleteNode(struct Node *s){
int ch,op;
do{
clrscr();
printf("1. Delete From Front");
printf("2. Delete From Mid");
printf("3. Delete From Last");
printf("Enter Choice : ");
scanf("%d",&ch);
switch(ch){
case 1: s = deleteFromFront(s);
break;
case 2: s = deleteFromMid(s);
break;
case 3: s = deleteFromLast(s);
break;
default: printf("Invalid Choice");
}
printf("Press Any Key..");
getch();
clrscr();
printf("Enter Nonzero value for repeat delete module otherwise enter zero : ");
scanf("%d",&op);
}while(op);
return s;
}
void main(){
int ch,op,i,n;
struct Node *s,*t,*p;
clrscr();
printf("Enter No of Node : ");
scanf("%d",&n);
s = allocate();
printf("Enter Data :");
scanf("%d",&s->data);
p=s;
for(i=1; i<n ; i++){
t = allocate();
printf("Enter Data :");
scanf("%d",&t->data);
p->next=t;
p=t;
}
do{
clrscr();
printf("1. Insert");
printf("2. Delete");
printf("3. Traverse");
printf("4. Reverse Traverse");
printf("5. Node Count");
printf("Enter Choice : ");
scanf("%d",&ch);
switch(ch){
case 1: s = insert(s);
break;
case 2: s = deleteNode(s);
break;
case 3: traverse(s);
break;
case 4: reverseTraverse(s);
break;
case 5: nodeCount(s);
break;
default: printf("Invalid Choice");
}
printf("Press Any Key..");
getch();
clrscr();
printf("Enter Nonzero value for repeat otherwise enter zero : ");
scanf("%d",&op);
}while(op);
}