Thursday, 10 September 2015

doubly link list

/*Program Name: Doubly Circular Linked List with different operations*/ 
  1. #include<iostream.h>
  2. #include<process.h>
  3. #include<conio.h>
  4.  
  5. struct cll
  6. {
  7.       int data;
  8.       struct cll*next;
  9.       struct cll*prev;
  10. };
  11.  
  12. class cll_create
  13. {
  14.       struct cll*head;
  15.  
  16.       public:
  17.       cll_create()
  18.       {
  19.             head=NULL;
  20.       }
  21.       void insert_at_end();
  22.       void insert_at_start();
  23.       void insert_between();
  24.       void display_cll();
  25. };
  26.  
  27. void cll_create::insert_between()
  28. {
  29.       struct cll*temp,*trav,*temp1;
  30.       int node;
  31.  
  32.       if(head==NULL)
  33.       {
  34.             head=new cll;
  35.             head->next=head;
  36.             head->prev=head;
  37.             cout<<"\nEnter the data";
  38.             cin>>head->data;
  39.       }
  40.       else
  41.       {
  42.             temp=new cll;
  43.             temp->next=NULL;
  44.             temp->prev=NULL;
  45.             cout<<"\nEnter the data";
  46.             cin>>temp->data;
  47.  
  48.             trav=head;
  49.  
  50.             cout<<"\nAfter which node you have to enter this node ";
  51.             cin>>node;
  52.             while(trav->data!=node&&trav->next!=head)
  53.                   trav=trav->next;
  54.  
  55.             if(trav->data==node)
  56.             {
  57.                   temp1=trav->next;
  58.  
  59.                   trav->next=temp;
  60.                   temp->prev=trav;
  61.  
  62.                   temp->next=temp1;
  63.                   temp1->prev=temp;
  64.             }
  65.             else
  66.             {
  67.                   cout<<"\nThe node not found in this CLL";
  68.             }
  69.  
  70.       }
  71. }
  72. void cll_create::insert_at_start()
  73. {
  74.       struct cll*temp,*trav;
  75.  
  76.       if(head==NULL)
  77.       {
  78.             head=new cll;
  79.             head->next=head;
  80.             head->prev=head;
  81.             cout<<"\nEnter the data";
  82.             cin>>head->data;
  83.       }
  84.       else
  85.       {
  86.             temp=new cll;
  87.             temp->next=NULL;
  88.             temp->prev=NULL;
  89.             cout<<"\nEnter the data";
  90.             cin>>temp->data;
  91.  
  92.             trav=head;
  93.  
  94.             while(trav->next!=head)
  95.                   trav=trav->next;
  96.  
  97.             trav->next=temp;
  98.             temp->prev=trav;
  99.  
  100.             temp->next=head;
  101.             head->prev=temp;
  102.  
  103.             head=temp;
  104.       }
  105. }
  106. void cll_create::insert_at_end()
  107. {
  108.       struct cll*temp,*trav;
  109.  
  110.       if(head==NULL)
  111.       {
  112.             head=new cll;
  113.             head->next=head;
  114.             head->prev=head;
  115.             cout<<"\nEnter the data";
  116.             cin>>head->data;
  117.       }
  118.       else
  119.       {
  120.             temp=new cll;
  121.             temp->next=NULL;
  122.             temp->prev=NULL;
  123.             cout<<"\nEnter the data";
  124.             cin>>temp->data;
  125.  
  126.             trav=head;
  127.  
  128.             while(trav->next!=head)
  129.             {
  130.                   trav=trav->next;
  131.             }
  132.             trav->next=temp;
  133.             temp->prev=trav;
  134.  
  135.             temp->next=head;
  136.             head->prev=temp;
  137.  
  138.       }
  139. }
  140.  
  141. void cll_create::display_cll()
  142. {
  143.       struct cll*trav;
  144.  
  145.       trav=head;
  146.  
  147.       cout<<"\n CLL is as follows :\n";
  148.  
  149.       while(trav->next!=head)
  150.       {
  151.             cout<<" "<<trav->data;
  152.             trav=trav->next;
  153.       }
  154.       cout<<" "<<trav->data;
  155. }
  156.  
  157. void main()
  158. {
  159.       cll_create object;
  160.       int choice;
  161.       clrscr();
  162.  
  163.       while(1)
  164.       {
  165.             cout<<"\n1.Insert data at end";
  166.             cout<<"\n2.Insert data at start";
  167.             cout<<"\n3.Insert between";
  168.             cout<<"\n4.Display CLL";
  169.             cout<<"\n5.Exit";
  170.             cout<<"\nEnter your choice ";
  171.             cin>>choice;
  172.  
  173.             switch(choice)
  174.             {
  175.                   case 1:
  176.                         object.insert_at_end();
  177.                         break;
  178.                   case 2:
  179.                         object.insert_at_start();
  180.                         break;
  181.                   case 3:
  182.                         object.insert_between();
  183.                         break;
  184.                   case 4:
  185.                         object.display_cll();
  186.                         break;
  187.                   case 5:
  188.                         exit(0);
  189.             }
  190.  
  191.       }
  192. }

No comments:

Post a Comment