© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies


CPP Program To Implement Double Linked List:

#include #include #include #define NULL 0 struct list { int data; struct list*next; struct list *prev; }; typedef struct list node; node *find(node *,int); node *pre=NULL; node *fin=NULL; node *start; int main() { int menu(void); void create(node *); void display(node *); void insert(node *,int,int); void reverse(node *); void del(node *,int); int choice,ch; int data,tar; node *newrec; start=NULL; clrscr(); do { choice=menu(); switch(choice) { case 1: cout<<"\nCreating the list"; cout<<"Enter the terms(type 0 to end)\n"; start=new node; create(start); break; case 2: if (start==NULL) cout<<"\nList does not exist"; else { cout<<"\nDisplaying the list\n"; display(start); } getch(); break; case 3: if (start==NULL) { cout<<"\nList does not exist"; getch(); } else { cout<<"\nDisplaying the list:"; reverse(fin); } break; case 4: if(start==NULL) { cout<<"\nList does not exist"; getch(); } else { cout<<"\nEnter the term to insert:"; cin>>data; cout<<"\nEnter target term:"; cin>>tar; insert(start,data,tar); } break; case 5: if(start==NULL) { cout<<"\nlist does not exist:"; getch(); } else { cout<<"\nEnter the term to delete:"; cin>>data; del(start,data); } break; case 6: exit(0); default: cout<<"\nNot a valid choice"; } }while(1); return(0); } int menu(void) { int ch; cout<<"\n1->Creation of the list"; cout<<"\n2->Displaying of the list"; cout<<"\n3->Reverse Displaying the list"; cout<<"\n4->Insertion of the list"; cout<<"\n5->Deletion of the list"; cout<<"\n6->Exit"; cout<<"\n\nEnter your choice:"; cin>>ch; return(ch); } void create(node *record) { cin>>record->data; if(record->data==0) { record->next=NULL; record->prev=pre; fin=record->prev; pre=NULL; } else { record->prev=pre; record->next=new node; pre=record; create(record->next); } return; } void display(node *record) { if(record->next!=NULL) { cout<data<<" "; display(record->next); } return; } void reverse(node *record) { if(record->prev!=NULL) { cout<data<<" "; reverse(record->prev); } return; } void insert(node *record,int data,int target) { node *tag,*newrec,*temp; newrec=new node; if(record->data==target) { newrec->next=record; newrec->prev=NULL; record->prev=newrec; start=newrec; } else { tag=find(record,target); temp=tag->prev; tag->prev=newrec; newrec->next=tag; temp->next=newrec; } if(tag==NULL) { cout<<"Target item not present in the list\n"; getch(); return; } newrec->data=data; return; } void del(node *record,int target) { node *tag,*temp; if(record->data==target) { temp=record; start=start->next; start->prev=NULL; delete temp; } else { tag=find(record,target); if(tag->next->next==NULL) { fin=fin->prev; fin->next=tag->next; } if(tag==NULL) { cout<<"Target item not present in the list\n"; getch(); return; } return; } } node *find(node *record,int target) { if(record->next->data==target) { return(record); } else if(record->next==NULL) return(NULL); else find(record->next,target); }

Sample Input Output:

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
   Enter your choice:1

   Enter the data(-999 to stop):
5
10
15
-999

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
   Enter your choice:2

New data item:20

Position of insertion:

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
   Enter your choice:4

   Forward traversal
   5  20  10  15  20
 
   Backward traversal
  20  15  10  20  5

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
   Enter your choice:3

  Enter the data to be deleted:5

Main menu

1.Create
2.Insert
3.Delete
4.Display
5.Exit
   Enter your choice:4

   Forward traversal
   20  10  15  20
 
   Backward traversal
  20  15  10  20 

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom