#include
#include
#include
#include
#include
void disp(struct node*);
struct node *addbeg(struct node *,int);
void addend(struct node *,int);
void sortlist(struct node*,int);
struct node *addbef(struct node *,int);
void addaft(struct node *,int);
void addbet(struct node *,int,int);
struct node *del(struct node *,int);
struct node *befdel(struct node *,int);
void aftdel(struct node *,int);
void betdel(struct node *,int,int);
void update(struct node *,int);
void search(struct node *,int);
struct node *reverse(struct node *);
struct node{ int n;
	 struct node *next;
	 } ;
void main()
{ char ch,boolc1,boolc2,boolc3,boolc4,boolc5,boolc6,boolc7;
 int
i,num,no,addb,adde,befadd,aftadd,fnode,snode,cut,befcut,aftcut,prnode,succ
node,change,find;
 struct node *head,*tail,*ptr;
 clrscr();
 printf("THIS IS A PROGRAM ABOUT LINKED LIST
");
 printf("supply no. of elements in the linked list
");
 scanf("%d",&num);
 head=tail=ptr=NULL;
 for(i=0;i
");
 scanf("%d",&no);
 ptr=(struct node*)malloc(sizeof(struct node));
 if(tail==NULL)
 { head=tail=ptr;
 ptr->n=no;
 ptr->next=NULL;
 }
 else
 { tail->next=ptr;
 ptr->n=no;
 ptr->next=NULL;
 tail=ptr;
 }
 }
 disp(head);
 printf("node you want to add before
");
 scanf("%d",&addb);
 if(addb>=0)
 { head=addbeg(head,addb);
 printf("Now");
 disp(head);
 }
 else printf("ayou don't! OK
");
 printf("node you want to add end
");
 scanf("%d",&adde);
 if(adde>=0)
 { addend(head,adde);
 printf("Now");
 disp(head);
 }
 else printf("ayou don't! OK
");
 printf("before which node you want to add?
");
 scanf("%d",&befadd);
 head=addbef(head,befadd);
 printf("Now");
 disp(head);
 printf("after which node you want to add?
");
 scanf("%d",&aftadd);
 addaft(head,aftadd);
 printf("Now");
 disp(head);
 printf("between which two nodes you want to add?
");
 fflush(stdin);
 scanf("%d %d",&fnode,&snode);
 addbet(head,fnode,snode);
 printf("Now");
 disp(head);
 printf("want to delete any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc1);
 if(boolc1=='y') { printf("supply node to be deleted
");
		 scanf("%d",&cut);
		 head=del(head,cut);
		 printf("Now");
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("want to delete before any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc2);
 if(boolc2=='y') { printf("supply that node
");
		 scanf("%d",&befcut);
		 head=befdel(head,befcut);
		 printf("Now");
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("want to delete after any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc3);
 if(boolc3=='y') { printf("supply that node
");
		 scanf("%d",&aftcut);
		 aftdel(head,aftcut);
		 printf("Now");
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("want to delete node between any two node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc4);
 if(boolc4=='y') { printf("supply those nodes
");
		 scanf("%d %d",&prnode,&succnode);
		 betdel(head,prnode,succnode);
		 printf("Now");
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("want to update any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc5);
 if(boolc5=='y') { printf("supply node to be updated
");
		 scanf("%d",&change);
		 update(head,change);
		 printf("Now");
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("want to search any node? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc6);
 if(boolc6=='y') { printf("node to be searched
");
		 scanf("%d",&find);
		 search(head,find);
		 }
 else printf("OK. list remains same
");
 printf("want to display the list in reverse order? (y/n)
");
 fflush(stdin);
 scanf("%c",&boolc7);
 if(boolc7=='y') { printf("In reverse order");
		 head=reverse(head);
		 disp(head);
		 }
 else printf("OK. list remains same
");
 printf("wish to sort the list? (y/n)
");
 fflush(stdin);
 scanf("%c",&ch);
 if(ch=='y')
 { sortlist(head,num);
 printf("after sorting");
 disp(head); }
 else{ printf("Finally");
	disp(head); }
 getch();
}
 void disp(struct node *head)
 { struct node *p;
 p=head;
 printf(" entire linked list is
");
 while(p->next!=NULL)
 { printf("%d->",p->n);
 p=p->next;
 if (p->next==NULL)
 printf("%d
",p->n);
 }
 return;
 }
 void sortlist(struct node *head,int num)
 { struct node *temp,*q;
 int i,j;
 q=head;
 temp=(struct node *)malloc(sizeof(struct node));
 for(i=0;i
 { if((q->n)>(q->next->n))
	 { temp->n=q->n;
	 q->n=q->next->n;
	 q->next->n=temp->n;
	 }
	 q=q->next;
 }
	if(q->next==NULL && i
 }
 q=head;
 return;
 }
 struct node *addbeg(struct node *head,int addn)
 { struct node *p;
 p=(struct node *)malloc(sizeof(struct node));
 p->n=addn;
 p->next=head;
 head=p;
 return head;
 }
 void addend(struct node *head,int addn)
 { struct node *p,*q;
 p=(struct node *)malloc(sizeof(struct node));
 q=head;
 while(q->next!=NULL)
 q=q->next;
 q->next=p;
 p->n=addn;
 p->next=NULL;
 return;
 }
 struct node *addbef(struct node *head,int befadd)
 { struct node *p,*q,*r;
 int addp;
 printf("new node
");
 scanf("%d",&addp);
 p=(struct node *)malloc(sizeof(struct node));
 p->n=addp;
 q=r=head;
 while(q->n!=befadd)
 { r=q;
 q=q->next;
 if(q==NULL) break;
 }
 if(q==NULL) { printf("anode %d not found
",befadd);
		 delay(1000);
		 return head;
	 }
 if(q==head) { p->next=q;
		 head=p;
		 return head;
	 }
 r->next=p;
 p->next=q;
 return head;
 }
 void addaft(struct node *head,int aftadd)
 { struct node *p,*q;
 int addp;
 printf("new node
");
 scanf("%d",&addp);
 p=(struct node *)malloc(sizeof(struct node));
 p->n=addp;
 q=head;
 while(q->n!=aftadd)
 { q=q->next;
 if(q==NULL) break;
 }
 if(q==NULL) { printf("anode %d not found
",aftadd);
		 delay(1000);
		 return;
	 }
 p->next=q->next;
 q->next=p;
 return;
 }
 void addbet(struct node *head,int no1,int no2)
 { struct node *p,*q,*r,*s;
 int addp;
 // printf("%d %d
",*no1,*no2);
 printf("new node
");
 scanf("%d",&addp);
 p=(struct node *)malloc(sizeof(struct node));
 p->n=addp;
 r=head;
 q=r;
 if(q->n!=no1)
 { r=q;
	q=q->next;
 }
 else
 { if (q->next->n!=no2)
	{ s=q->next;
	 while(s!=NULL) { s=s->next;
				if(s->n==no2)
			 { printf("anodes are not successive
");
				delay(1000);
				return;
			 }
			 }
	 printf("aillegal node selection
");
	 delay(1000);
	 return;
	}
 else { q=q->next;
	 r->next=p;
	 p->next=q;
	 return;
	 }
 }
 while(r->n!=no1 || q->n!=no2)
 { r=q;
 q=q->next;
 if(q==NULL)
 { printf("aillegal node selection
");
	 delay(1000);
	 return;
 }
 }
 r->next=p;
 p->next=q;
 return;
 }
struct node *del(struct node *head,int cut)
 { struct node *p,*q;
 p=head;
 q=p;
 while(p->n!=cut)
 { q=p;
 p=p->next;
 if(p==NULL) { printf("anode %d not found
",cut);
		 delay(1000);
		 return head;
		 }
 }
 if(p==head) { head=q->next;
		 q=head;
		 free(p);
		 return head;
	 }
 q->next=p->next;
 free(p);
 return head;
 }
 struct node *befdel(struct node *head,int befcut)
 { struct node *p,*q;
 p=head;
 q=p;
 while(p->next->n!=befcut)
 { q=p;
 p=p->next;
 if(p==NULL) { printf("anode %d not found
",befcut);
		 delay(1000);
		 return head;
		 }
 }
 if(p==head) { head=q->next;
		 q=head;
		 free(p);
		 return head;
	 }
 q->next=p->next;
 free(p);
 return head;
 }
void aftdel(struct node *head,int aftcut)
{ struct node *p,*q;
 p=head;
 q=p;
 while(q->n!=aftcut)
 { q=p;
 p=p->next;
 if(p==NULL) { if(q->next==NULL) printf("ano node after node
%d
",aftcut);
		 else printf("anode %d not found
",aftcut);
		 delay(1000);
		 return;
		}
 }
 if(q==head) p=q->next;
 q->next=p->next;
 free(p);
 return;
 }
void betdel(struct node *head,int prnode,int succnode)
{ struct node *p,*q,*r,*s;
 p=head;
 q=p;
 if(p->n!=prnode)
 { q=p;
 p=p->next;
 }
 else { r=p->next;
	 if(r->next->n!=succnode)
	 { s=r->next;
	 while(s!=NULL) { s=s->next;
			 if(s->n==succnode)
			 { printf("amore nodes between those nodes
");
			 delay(1000);
			 return;
			 }
			 }
	 printf("aillegal node selection
");
	 delay(1000);
	 return;
	 }
	 else { q->next=r->next;
		 free(r);
		 return;
		}
 }
 while(q->n!=prnode || p->next->n!=succnode)
 { q=p;
 p=p->next;
 if(p->next==NULL) { printf("aillegal node selection
");
			 delay(1000);
			 return;
		 }
 }
 q->next=p->next;
 free(p);
 return;
 }
 void update(struct node *head,int change)
 { struct node *p;
 int upd;
 p=head;
 printf("updated node
");
 scanf("%d",&upd);
 while(p->n!=change)
 { p=p->next;
 if(p==NULL) { printf("anode %d not found
",change);
		 delay(1000);
		 return;
	 }
 }
 p->n=upd;
 return;
 }
 void search(struct node *head,int find)
 { struct node *p;
 int j=1;
 p=head;
 while(p->n!=find)
 { p=p->next;
 j++;
 if(p==NULL) { printf("
SORRY. node %d is not present
",find);
		 delay(1000);
		 return;
		 }
 }
 printf("aYES! the node %d is present in the %dth 
position
",find,j);
 delay(1000);
 return;
 }
 struct node *reverse(struct node *head)
 { struct node *t1,*t2;
 t1=head->next;
 t2=t1->next;
 head->next=NULL;
 while(t1!=NULL)
 { t1->next=head;
 head=t1;
 t1=t2;
 t2=t2->next;
 }
 return head;
 }