#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *prev; struct node *next; }; struct node* root = NULL ; int count=0; void create(void); void append(void); void addFirst(void); void addAfter(void); void display(void); void delete(void); void main() { int ch; while(1) { printf(“Double LinkedList Operations\n”); printf(“1.Create\n”); printf(“2.Append\n”); printf(“3.AddFirst\n”); printf(“4.AddAfter\n”); printf(“5.Delete\n”); printf(“6.Display\n”); printf(“7.Exit\n”); printf(“Enter Choice : “); scanf(“%d”,&ch); switch(ch) { case 1 : create(); break; case 2 : append(); break; case 3 : addFirst(); break; case 4 : addAfter(); break; case 5 : delete(); break; case 6 : display(); break; case 7 : exit(1); default : printf(“Invalid choice\n\n”); } } } void create() { int ch; struct node *p,*temp; while(1) { p=(struct node*)malloc(sizeof(struct node)); printf(“Enter data : “); scanf(“%d”,&p->data); p->prev=NULL; p->next=NULL; count++; if(root==NULL) { root=p; temp=p; } else { temp->next=p; p->prev=temp; temp=p; p->next=NULL; } printf(“Do you want to continue(y/n):\n”); if(getche()==’n’) { break; } } } void append() { struct node *temp,*p; p=(struct node *)malloc(sizeof(struct node)); printf(“Enter data:”); scanf(“%d”,&p->data); count++; if(root==NULL) { root=p; temp=p; } else { temp = root ; while(temp->next!=NULL) { temp=temp->next; } temp->next=p; p->prev=temp; p->next=NULL; } } void addFirst() { struct node *p; p=(struct node *)malloc(sizeof(struct node)); printf(“Enter the data:”); scanf(“%d”,&p->data); count++; if(root==NULL) root=p; else { root->prev=p; p->next=root; root=p; p->prev=NULL; } } void addAfter() { struct node *temp,*p; int i=1,loc; temp=root; printf(“Enter the location:”); scanf(“%d”,&loc); if(loc>count) { printf(“Invalid location\n”); printf(“Currently List having %d nodes\n\n”,count); } else { while((i<loc)) { temp=temp->next; i++; } p=(struct node *)malloc(sizeof(struct node)); printf(“Enter data:”); scanf(“%d”,&p->data); count++; p->next=temp->next; temp->next->prev=p; temp->next = p; p->prev=temp; } } void delete() { int loc,i=1; struct node *temp,*u; temp=root; printf(“Enter location:”); scanf(“%d”,&loc); if(loc>count) printf(“Currently %d nodes in the list\n\n”,count); else if(loc==1) { root=temp->next; temp->next=NULL; root->prev=NULL; free(temp); count–; } else { while((i<loc-1)) { temp=temp->next; i++; } u=temp->next->next; temp->next=u; u->prev=temp; count–; } } void display() { struct node *temp; temp=root; if(temp==NULL) printf(“Empty list\n\n”); else { printf(“List elements are :\n”); while(temp!=NULL) { printf(“%d\n”,temp->data); temp=temp->next; } } } |