C++ Program to Implement Pagoda

«
»
This C++ Program demonstrates operations on Pagoda.

Here is source code of the C++ Program to demonstrate Pagoda. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Pagoda
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. #define MAX_ELEMS 25
  7. using namespace std;
  8. /*
  9.  * Node Declaration
  10.  */
  11. struct node
  12. {
  13.     int k;
  14.     node *left;
  15.     node *right;
  16. };
  17. typedef node *tree;
  18.  
  19. /*
  20.  * Merging two trees
  21.  */
  22. tree merge(tree a,tree b )
  23. {
  24.     tree bota, botb, r, temp;
  25.     if (a == NULL) 
  26.         return b;
  27.     else if (b == NULL) 
  28.         return a;
  29.     else	
  30.     {
  31.         bota = a->right;
  32.         a->right = NULL;
  33.         botb = b->left;	
  34.         b->left = NULL;
  35.         r = NULL;
  36.         while (bota != NULL && botb != NULL)
  37.         {
  38.             if (bota->k < botb->k) 
  39.             {
  40.                 temp = bota->right;
  41.                 if (r == NULL)	
  42.                 bota->right = bota;
  43.                 else	
  44.                 {
  45.                     bota->right = r->right;
  46.                     r->right = bota;
  47.                 }
  48.                 r = bota;
  49.                 bota = temp;
  50.             }
  51.             else	
  52.             {
  53.                 temp = botb->left;
  54.                 if (r == NULL)	
  55.                     botb->left = botb;
  56.                 else	
  57.                 {
  58.                     botb->left = r->left;
  59.                     r->left = botb;
  60.                 }
  61.                 r = botb;
  62.                 botb = temp;
  63.             }
  64.         }
  65.         if (botb == NULL) 
  66.         {
  67.             a->right = r->right;
  68.             r->right = bota;
  69.             return a;
  70.         }
  71.         else
  72.         {
  73.             b->left = r->left;
  74.             r->left = botb;
  75.             return b;
  76.         }
  77.     }
  78. }
  79. /*
  80.  * Insert element into pagoda
  81.  */
  82. tree insert(tree node, tree pq)
  83. {
  84.     node->left = node;
  85.     node->right = node;
  86.     return merge(pq, node);
  87. }
  88.  
  89. /*
  90.  * Delete element from pagoda
  91.  */
  92. tree del(tree pq)
  93. {
  94.     tree le, ri;
  95.     if (pq == NULL)
  96.     {
  97.         cout<<"Deletion out of range"<<endl;
  98.         return NULL;
  99.     }
  100.     else	
  101.     {	
  102.         if (pq->left == pq) 
  103.             le = NULL;
  104.         else 
  105.         {
  106.             le = pq->left;
  107.             while (le->left != pq) 
  108.             le = le->left;
  109.             le->left = pq->left;
  110.         }
  111.         if (pq->right == pq) 
  112.             ri = NULL;
  113.         else 
  114.         {
  115.             ri = pq->right;
  116.             while (ri->right != pq) 
  117.                 ri = ri->right;
  118.             ri->right = pq->right;
  119.         }
  120.         return merge(le, ri);
  121.     }
  122. }
  123.  
  124. /*
  125.  * Main Contains Menu
  126.  */
  127. int main()
  128. {
  129.     node **root = new node* [MAX_ELEMS + 1];
  130.     int value, i = 0;
  131.     int choice;
  132.     while(1)
  133.     {
  134.         cout<<"\n----------------------"<<endl;
  135.         cout<<"Operations on Pagoda"<<endl;
  136.         cout<<"\n----------------------"<<endl;
  137.         cout<<"1.Insert element at Last"<<endl;
  138.         cout<<"2.Display Pagoda"<<endl;
  139.         cout<<"3.Delete Last element"<<endl;
  140.         cout<<"4.Exit"<<endl;
  141.         cout<<"Enter your choice: ";
  142.         cin>>choice;
  143.         switch(choice)
  144.         {
  145.         case 1:
  146.         {
  147.             cout<<"Enter element "<<i + 1<<": ";
  148.             cin>>value;
  149.             node *temp = new node;
  150.             temp->k = value;
  151.             root[i] = insert(temp, root[i]);
  152.             i++;
  153.             break;
  154.         }
  155.         case 2:
  156.         {
  157.             cout<<"Displaying elements of pagoda: "<<endl;
  158.             int j = 0;
  159.             while(root[j] != NULL)
  160.             {    
  161. 	        cout<<"Element "<<j + 1<<": "<<root[j]->k<<endl;
  162. 	        j++;
  163. 	    }
  164.             break;
  165.         }
  166.         case 3:
  167.             root[i - 1] = del(root[i - 1]);
  168.             i--;
  169.             break;
  170.         case 4:
  171.             exit(1);
  172.         default:
  173.            cout<<"Wrong Choice"<<endl;
  174.        }
  175.     }
  176.     return 0;
  177. }

advertisement
$ g++ pagoda.cpp
$ a.out
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 1: 5
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 2
Displaying elements of pagoda: 
Element 1: 5
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 2: 4
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 3: 6
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 4: 8
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 5: 11
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 2
Displaying elements of pagoda: 
Element 1: 5
Element 2: 4
Element 3: 6
Element 4: 8
Element 5: 11
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 3
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 2
Displaying elements of pagoda: 
Element 1: 5
Element 2: 4
Element 3: 6
Element 4: 8
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 5: 15
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 3
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 2
Displaying elements of pagoda: 
Element 1: 5
Element 2: 4
Element 3: 6
Element 4: 8
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 1
Enter element 5: 100
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 2
Displaying elements of pagoda: 
Element 1: 5
Element 2: 4
Element 3: 6
Element 4: 8
Element 5: 100
 
----------------------
Operations on Pagoda
 
----------------------
1.Insert element at Last
2.Display Pagoda
3.Delete Last element
4.Exit
Enter your choice: 4
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn