Skip to main content

C++ Program for Circular Doubly Linked List

Today we are gonna tell you the C++ Program to demonstrate circular doubly linked list. The C++ program is successfully compiled and runs on any system. 


Here is the code :


  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. /*
  40.  * Main: Contains Menu
  41.  */
  42. int main()
  43. {
  44.     int choice;
  45.     double_clist cdl;
  46.     while (1)
  47.     {
  48.         cout<<"\n-------------------------------"<<endl;
  49.         cout<<"Operations on Doubly Circular linked list"<<endl;
  50.         cout<<"\n-------------------------------"<<endl;         
  51.         cout<<"1.Insert at Beginning"<<endl;
  52.         cout<<"2.Insert at Last"<<endl;
  53.         cout<<"3.Insert at Position"<<endl;
  54.         cout<<"4.Delete at Position"<<endl;
  55.         cout<<"5.Update Node"<<endl;
  56.         cout<<"6.Search Element"<<endl;
  57.         cout<<"7.Sort"<<endl;
  58.         cout<<"8.Display List"<<endl;
  59.         cout<<"9.Reverse List"<<endl;
  60.         cout<<"10.Exit"<<endl;
  61.         cout<<"Enter your choice : ";
  62.         cin>>choice;
  63.         switch(choice)
  64.         {
  65.         case 1:
  66.             cdl.insert_begin();
  67.             break;
  68.         case 2:
  69.             cdl.insert_last();
  70.             break;   
  71.         case 3:
  72.             cdl.insert_pos();
  73.             break; 
  74.         case 4:
  75.             cdl.delete_pos();
  76.             break;
  77.         case 5:
  78.             cdl.update();
  79.             break;
  80.         case 6:
  81.             cdl.search();
  82.             break;
  83.         case 7:
  84.             cdl.sort();
  85.             break;
  86.         case 8:
  87.             cdl.display();
  88.             break;
  89.         case 9:
  90.             cdl.reverse();
  91.             break;
  92.         case 10:
  93.             exit(1); 
  94.         default:
  95.             cout<<"Wrong choice"<<endl; 
  96.         }
  97.     }
  98.     return 0;
  99. }
  100.  
  101. /*
  102.  *MEMORY ALLOCATED FOR NODE DYNAMICALLY
  103.  */
  104. node* double_clist::create_node(int value)
  105. {
  106.     counter++;  
  107.     struct node *temp;
  108.     temp = new(struct node);
  109.     temp->info = value;
  110.     temp->next = NULL;
  111.     temp->prev = NULL;
  112.     return temp;
  113. }
  114. /*
  115.  *INSERTS ELEMENT AT BEGINNING
  116.  */
  117. void double_clist::insert_begin()
  118. {
  119.     int value;
  120.     cout<<endl<<"Enter the element to be inserted: ";
  121.     cin>>value;
  122.     struct node *temp;
  123.     temp = create_node(value);
  124.     if (start == last && start == NULL)
  125.     {    
  126.         cout<<"Element inserted in empty list"<<endl;
  127.         start = last = temp;
  128.         start->next = last->next = NULL;
  129.         start->prev = last->prev = NULL;
  130.     }
  131.     else
  132.     {
  133.         temp->next = start;
  134.         start->prev = temp;
  135.         start = temp;
  136.         start->prev = last;
  137.         last->next = start;
  138.         cout<<"Element inserted"<<endl;
  139.     }
  140. }
  141.  
  142. /*
  143.  *INSERTS ELEMNET AT LAST
  144.  */
  145. void double_clist::insert_last()
  146. {
  147.     int value;    
  148.     cout<<endl<<"Enter the element to be inserted: ";
  149.     cin>>value; 
  150.     struct node *temp;
  151.     temp = create_node(value);
  152.     if (start == last && start == NULL)
  153.     {
  154.         cout<<"Element inserted in empty list"<<endl;
  155.         start = last = temp;
  156.         start->next = last->next = NULL;    
  157.         start->prev = last->prev = NULL;
  158.     }
  159.     else
  160.     {
  161.         last->next = temp;
  162.         temp->prev = last;
  163.         last = temp;
  164.         start->prev = last;
  165.         last->next = start;
  166.     }
  167. }
  168. /*
  169.  *INSERTS ELEMENT AT POSITION
  170.  */
  171. void double_clist::insert_pos()
  172. {    
  173.     int value, pos, i;
  174.     cout<<endl<<"Enter the element to be inserted: ";
  175.     cin>>value;
  176.     cout<<endl<<"Enter the postion of element inserted: ";
  177.     cin>>pos;
  178.     struct node *temp, *s, *ptr;
  179.     temp = create_node(value);
  180.     if (start == last && start == NULL)
  181.     {
  182.         if (pos == 1)
  183.         {
  184.             start = last = temp;
  185.             start->next = last->next = NULL;    
  186.             start->prev = last->prev = NULL;
  187.         }
  188.         else
  189.         {
  190.             cout<<"Position out of range"<<endl;
  191.             counter--;
  192.             return;
  193.         }
  194.     }  
  195.     else
  196.     {
  197.         if (counter < pos)
  198.         {
  199.              cout<<"Position out of range"<<endl;
  200.              counter--;
  201.              return;     
  202.         }
  203.         s = start;  
  204.         for (i = 1;i <= counter;i++)
  205.         {
  206.             ptr = s;
  207.             s = s->next;
  208.             if (i == pos - 1)
  209.             {
  210.                 ptr->next = temp;
  211.                 temp->prev = ptr;
  212.                 temp->next = s;
  213.                 s->prev = temp;
  214.                 cout<<"Element inserted"<<endl;
  215.                 break;
  216.             }
  217.         }
  218.     }
  219. }
  220. /*
  221.  * Delete Node at Particular Position
  222.  */
  223. void double_clist::delete_pos()
  224. {    
  225.     int pos, i;
  226.     node *ptr, *s;
  227.     if (start == last && start == NULL)
  228.     {
  229.         cout<<"List is empty, nothing to delete"<<endl;
  230.         return;
  231.     }
  232.     cout<<endl<<"Enter the postion of element to be deleted: ";
  233.     cin>>pos;
  234.     if (counter < pos)
  235.     {
  236.         cout<<"Position out of range"<<endl;
  237.         return;
  238.     }
  239.     s = start;
  240.     if(pos == 1)
  241.     {
  242.         counter--;
  243.         last->next = s->next;
  244.         s->next->prev = last;
  245.         start = s->next;
  246.         free(s);
  247.         cout<<"Element Deleted"<<endl;
  248.         return;    
  249.     }
  250.     for (i = 0;i < pos - 1;i++ )
  251.     {  
  252.         s = s->next;
  253.         ptr = s->prev;       
  254.     }    
  255.     ptr->next = s->next;
  256.     s->next->prev = ptr;
  257.     if (pos == counter)
  258.     {
  259.         last = ptr;     
  260.     }
  261.     counter--;
  262.     free(s);
  263.     cout<<"Element Deleted"<<endl;
  264. }
  265. /*
  266.  * Update value of a particular node 
  267.  */
  268. void double_clist::update()
  269. {
  270.     int value, i, pos;
  271.     if (start == last && start == NULL)
  272.     {
  273.         cout<<"The List is empty, nothing to update"<<endl;
  274.         return; 
  275.     }
  276.     cout<<endl<<"Enter the postion of node to be updated: ";
  277.     cin>>pos;
  278.     cout<<"Enter the new value: ";
  279.     cin>>value;
  280.     struct node *s;
  281.     if (counter < pos)
  282.     {
  283.         cout<<"Position out of range"<<endl;
  284.         return;
  285.     }
  286.     s = start;  
  287.     if (pos == 1)
  288.     {
  289.        s->info = value;
  290.        cout<<"Node Updated"<<endl;
  291.        return;   
  292.     }
  293.     for (i=0;i < pos - 1;i++)
  294.     {
  295.         s = s->next;    
  296.     }
  297.     s->info = value;
  298.     cout<<"Node Updated"<<endl;
  299. }
  300. /*
  301.  * Search Element in the list
  302.  */
  303. void double_clist::search()
  304. {
  305.     int pos = 0, value, i;
  306.     bool flag = false;
  307.     struct node *s;
  308.     if (start == last && start == NULL)
  309.     {
  310.         cout<<"The List is empty, nothing to search"<<endl;
  311.         return;
  312.     }
  313.     cout<<endl<<"Enter the value to be searched: ";
  314.     cin>>value;
  315.     s = start;
  316.     for (i = 0;i < counter;i++)
  317.     {
  318.         pos++;
  319.         if (s->info == value)
  320.         {
  321.             cout<<"Element "<<value<<" found at position: "<<pos<<endl;
  322.             flag = true;
  323.         }    
  324.         s = s->next;
  325.     }
  326.     if (!flag)
  327.         cout<<"Element not found in the list"<<endl;   
  328. }
  329. /*
  330.  * Sorting Doubly Circular Link List
  331.  */
  332. void double_clist::sort()
  333. {
  334.     struct node *temp, *s;
  335.     int value, i;
  336.     if (start == last && start == NULL)
  337.     {
  338.         cout<<"The List is empty, nothing to sort"<<endl;
  339.         return;
  340.     }
  341.     s = start;
  342.     for (i = 0;i < counter;i++)
  343.     {
  344.         temp = s->next;
  345.         while (temp != start)
  346.         {
  347.             if (s->info > temp->info)
  348.             {
  349.                 value = s->info;
  350.                 s->info = temp->info;
  351.                 temp->info = value;
  352.             }
  353.             temp = temp->next;
  354.         }
  355.         s = s->next;
  356.     }
  357. }
  358. /*
  359.  * Display Elements of the List 
  360.  */
  361. void double_clist::display()
  362. {
  363.     int i;
  364.     struct node *s;
  365.     if (start == last && start == NULL)
  366.     {
  367.         cout<<"The List is empty, nothing to display"<<endl;
  368.         return;
  369.     }
  370.     s = start;
  371.     for (i = 0;i < counter-1;i++)
  372.     { 
  373.         cout<<s->info<<"<->";
  374.         s = s->next; 
  375.     }
  376.     cout<<s->info<<endl;
  377. }
  378. /*
  379.  * Reverse Doubly Circular Linked List 
  380.  */
  381. void double_clist::reverse()
  382. {
  383.     if (start == last && start == NULL)
  384.     {
  385.         cout<<"The List is empty, nothing to reverse"<<endl;
  386.         return;
  387.     }
  388.     struct node *p1, *p2;
  389.     p1 = start;
  390.     p2 = p1->next;
  391.     p1->next = NULL;
  392.     p1->prev = p2;
  393.     while (p2 != start)
  394.     {
  395.         p2->prev = p2->next;
  396.         p2->next = p1;
  397.         p1 = p2;
  398.         p2 = p2->prev;
  399.     }
  400.     last = start;
  401.     start = p1;
  402.     cout<<"List Reversed"<<endl;   
  403. }

OUTPUT (screenshot):








Comments

Popular posts from this blog

Android secret codes

Just type these codes as it is in your phone and see the magic!!!! 1. Phone Information, Usage and Battery –  *#*#4636#*#* 2. IMEI Number – *#06# 3. Enter Service Menu On Newer Phones – *#0*# 4. Detailed Camera Information –  *#*#34971539#*#* 5. Backup All Media Files – *#*#273282*255* 663282*#*#* 6. Wireless LAN Test – *#*#232339#*#* 7. Enable Test Mode for Service –*#*#197328640#*#* 8. Back-light Test – *#*#0842#*#* 9. Test the Touchscreen – *#*#2664#*#* 10. Vibration Test – *#*#0842#*#* 11. FTA Software Version – *#*#1111#*#* 12. Complete Software and Hardware Info –*#12580*369# 13. Diagnostic Configuration – *#9090# 14. USB Logging Control – *#872564# 15. System Dump Mode – *#9900# 16. HSDPA/HSUPA Control Menu – *#301279# 17. View Phone Lock Status – *#7465625# 18. Reset the Data Partition to Factory State –*#*#7780#*#* 19. Format Your Device To Factory State(will delete everything on your phone) – *2767*3855# 20. Hidden Service Menu For Mot

Finding IP address using facebook!!!!!

If you want to check for ip address of particular person on facebook or orkut or any other social site just invite them for a chat, so that your browser should connect to that system, than only when chat is ON open command prompt type the below command Netstat –an This will show you the  connected ip addresses, than from shown ip addresses search for suspicious ip address that is not the local connection address. Local connections normally start from 192.168.1.1 ranging to 192.168.1.255 Other netstat commands: -a Displays all connections and listening ports. -e Displays Ethernet statistics. This may be combined with the -s option. -n Displays addresses and port numbers in numerical form. -p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP. -s option to display per-protocol statistics, proto may be TCP, UDP, or IP. -r Displays the routing table. -s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP; the -p option