The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Circulardoublylinkedlist
By Guest on 11th December 2023 06:25:45 AM | Syntax: TEXT | Views: 96



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6. /*
  7.  * Node Declaration
  8.  */
  9. struct node
  10. {
  11.     int info;
  12.     struct node *next;
  13.     struct node *prev;
  14. }*start, *last;
  15. int counter = 0;
  16. /*
  17.  * Class Declaration
  18.  */
  19. class double_clist
  20. {
  21.     public:
  22.         node *create_node(int);
  23.         void insert_begin();
  24.         void insert_last();
  25.         void insert_pos();
  26.         void delete_pos();
  27.         void search();
  28.         void update();
  29.         void display();
  30.         void reverse();
  31.         void sort();
  32.         double_clist()
  33.         {
  34.             start = NULL;
  35.             last = NULL;                       
  36.         }
  37. };
  38.  
  39. int main()
  40. {
  41.     int choice;
  42.     double_clist cdl;
  43.     while (1)
  44.     {
  45.         cout<<"\n-------------------------------"<<endl;
  46.         cout<<"Operations on Doubly Circular linked list"<<endl;
  47.         cout<<"\n-------------------------------"<<endl;        
  48.         cout<<"1.Insert at Beginning"<<endl;
  49.         cout<<"2.Insert at Last"<<endl;
  50.         cout<<"3.Insert at Position"<<endl;
  51.         cout<<"4.Delete at Position"<<endl;
  52.         cout<<"5.Display List"<<endl;
  53.         cout<<"6.Exit"<<endl;
  54.         cout<<"Enter your choice : ";
  55.         cin>>choice;
  56.                 switch(choice)
  57.         {
  58.         case 1:
  59.             cdl.insert_begin();
  60.             break;
  61.         case 2:
  62.             cdl.insert_last();
  63.             break;  
  64.         case 3:
  65.             cdl.insert_pos();
  66.             break;
  67.         case 4:
  68.             cdl.delete_pos();
  69.             break;
  70.                 case 5:
  71.             cdl.display();
  72.             break;
  73.         case 6:
  74.             exit(1);
  75.         default:
  76.             cout<<"Wrong choice"<<endl;
  77.         }
  78.     }
  79.     return 0;
  80. }
  81.  
  82.  
  83. node* double_clist::create_node(int value)
  84. {
  85.     counter++;  
  86.     struct node *temp;
  87.     temp = new(struct node);
  88.     temp->info = value;
  89.     temp->next = NULL;
  90.     temp->prev = NULL;
  91.     return temp;
  92. }
  93. /*
  94.  *INSERTS ELEMENT AT BEGINNING
  95.  */
  96. void double_clist::insert_begin()
  97. {
  98.     int value;
  99.     cout<<endl<<"Enter the element to be inserted: ";
  100.     cin>>value;
  101.     struct node *temp;
  102.      temp = create_node(value);
  103.     if (start == last && start == NULL)
  104.     {    
  105.         cout<<"Element inserted in empty list"<<endl;
  106.         start = last = temp;
  107.         start->next = last->next = NULL;
  108.         start->prev = last->prev = NULL;
  109.     }
  110.     else
  111.     {
  112.         temp->next = start;
  113.         start->prev = temp;
  114.         start = temp;
  115.         start->prev = last;
  116.         last->next = start;
  117.         cout<<"Element inserted"<<endl;
  118.     }
  119. }
  120. void double_clist::insert_last()
  121. {
  122.     int value;    
  123.     cout<<endl<<"Enter the element to be inserted: ";
  124.     cin>>value;
  125.     struct node *temp;
  126.     temp = create_node(value);
  127.     if (start == last && start == NULL)
  128.     {
  129.         cout<<"Element inserted in empty list"<<endl;
  130.         start = last = temp;
  131.         start->next = last->next = NULL;    
  132.         start->prev = last->prev = NULL;
  133.     }
  134.     else
  135.     {
  136.         last->next = temp;
  137.         temp->prev = last;
  138.         last = temp;
  139.         start->prev = last;
  140.         last->next = start;
  141.     }
  142. }
  143.  
  144. /*
  145.  *INSERTS ELEMENT AT POSITION
  146.  */
  147. void double_clist::insert_pos()
  148. {    
  149.     int value, pos, i;
  150.     cout<<endl<<"Enter the element to be inserted: ";
  151.     cin>>value;
  152.     cout<<endl<<"Enter the postion of element inserted: ";
  153.     cin>>pos;
  154.     struct node *temp, *s, *ptr;
  155.     temp = create_node(value);
  156.     if (start == last && start == NULL)
  157.     {
  158.         if (pos == 1)
  159.         {
  160.             start = last = temp;
  161.             start->next = last->next = NULL;    
  162.             start->prev = last->prev = NULL;
  163.         }
  164.         else
  165.         {
  166.             cout<<"Position out of range"<<endl;
  167.             counter--;
  168.                    return;
  169.         }
  170.     }  
  171.     else
  172.     {
  173.         if (counter < pos)
  174.         {
  175.              cout<<"Position out of range"<<endl;
  176.              counter--;
  177.              return;           
  178.         }
  179.         s = start;             
  180.         for (i = 1;i <= counter;i++)
  181.         {
  182.             ptr = s;
  183.             s = s->next;
  184.             if (i == pos - 1)
  185.             {
  186.                 ptr->next = temp;
  187.                 temp->prev = ptr;
  188.                 temp->next = s;
  189.                 s->prev = temp;
  190.                 cout<<"Element inserted"<<endl;
  191.                  break;
  192.             }
  193.         }
  194.     }
  195. }
  196. /*
  197.  * Delete Node at Particular Position
  198.  */
  199. void double_clist::delete_pos()
  200. {    
  201.     int pos, i;
  202.     node *ptr, *s;
  203.     if (start == last && start == NULL)
  204.     {
  205.         cout<<"List is empty, nothing to delete"<<endl;
  206.         return;
  207.     }
  208.     cout<<endl<<"Enter the postion of element to be deleted: ";
  209.     cin>>pos;
  210.     if (counter < pos)
  211.     {
  212.         cout<<"Position out of range"<<endl;
  213.         return;
  214.     }
  215.     s = start;
  216.       if(pos == 1)
  217.     {
  218.         counter--;
  219.         last->next = s->next;
  220.         s->next->prev = last;
  221.         start = s->next;
  222.         free(s);
  223.         cout<<"Element Deleted"<<endl;
  224.         return;    
  225.     }
  226.     for (i = 0;i < pos - 1;i++ )
  227.     {  
  228.         s = s->next;
  229.         ptr = s->prev;           
  230.     }    
  231.     ptr->next = s->next;
  232.     s->next->prev = ptr;
  233.     if (pos == counter)
  234.     {
  235.         last = ptr;        
  236.     }
  237.     counter--;
  238.     free(s);
  239.     cout<<"Element Deleted"<<endl;
  240. }
  241.  
  242. /*
  243.  * Display Elements of the List
  244.  */
  245. void double_clist::display()
  246. {
  247.     int i;
  248.     struct node *s;
  249.     if (start == last && start == NULL)
  250.     {
  251.         cout<<"The List is empty, nothing to display"<<endl;
  252.         return;
  253.     }
  254.     s = start;
  255.     for (i = 0;i < counter-1;i++)
  256.     {  
  257.         cout<<s->info<<"<->";
  258.         s = s->next;
  259.     }
  260.     cout<<s->info<<endl;
  261. }