C++ Program to Implement Stack using Two Queues

«
»
This C++ program implements a stack data structure using two queue data structures. A stack data structure foolows the principle of LIFO(Last Element in First Element Out).

Here is the source code of the C program to displays the popped values from a stack in LIFO mode. The C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Stack using Two Queues
  3.  */
  4. #include<stdio.h>
  5. #include<iostream>
  6. #include<conio.h>
  7. using namespace std;
  8. struct queue1
  9. {
  10.     queue1 *next1;
  11.     int data1;
  12. }*front1 = NULL, *rear1 = NULL, *q1 = NULL, *p1 = NULL, *np1 = NULL;
  13. struct queue2
  14. {
  15.     queue2 *next2;
  16.     int data2;
  17. }*front2 = NULL, *rear2 = NULL, *q2 = NULL, *p2 = NULL, *np2 = NULL;
  18. void enqueue1(int x)
  19. {     
  20.     np1 = new queue1;
  21.     np1->data1 = x;
  22.     np1->next1 = NULL;
  23.     if (front1 == NULL)
  24.     {
  25.         rear1 = np1;
  26.         rear1->next1 = NULL;
  27.         front1 = rear1;
  28.     }
  29.     else
  30.     {
  31.         rear1->next1 = np1;
  32.         rear1 = np1;
  33.         rear1->next1 = NULL;
  34.     }
  35. }
  36. int dequeue1()
  37. {
  38.     int x;
  39.     if (front1 == NULL)
  40.     {
  41.         cout<<"no elements present in queue\n";
  42.     }
  43.     else
  44.     {
  45.         q1 = front1;
  46.         front1 = front1->next1;
  47.         x = q1->data1;
  48.         delete(q1);
  49.         return x;
  50.     }
  51. }
  52. void enqueue2(int x)
  53. {
  54.     np2 = new queue2;
  55.     np2->data2 = x;
  56.     np2->next2 = NULL;
  57.     if (front2 == NULL)
  58.     {
  59.         rear2 = np2;
  60.         rear2->next2 = NULL;
  61.         front2=rear2;
  62.     }
  63.     else
  64.     {
  65.         rear2->next2 = np2;
  66.         rear2 = np2;
  67.         rear2->next2 = NULL;
  68.     }
  69. }
  70. int dequeue2()
  71. {
  72.     int x;
  73.     if (front2 == NULL)
  74.     {
  75.         cout<<"no elements present in queue\n";
  76.     }
  77.     else
  78.     {
  79.         q2 = front2;
  80.         front2 = front2->next2;
  81.         x = q2->data2;
  82.         delete(q2);
  83.         return x;
  84.     }
  85. }        
  86. int main()
  87. {
  88.     int n, x, i = 0;
  89.     cout<<"Enter the number of elements to be entered into stack\n";
  90.     cin>>n;
  91.     while (i < n)
  92.     {
  93.         cout<<"enter the element to be entered\n";
  94.         cin>>x;
  95.         enqueue1(x);
  96.         i++;
  97.     }
  98.     cout<<"\n\nElements popped\n\n";
  99.     while (front1 != NULL || front2 != NULL)
  100.     {
  101.         if (front2 == NULL)
  102.         {
  103.             while (front1->next1 != NULL)
  104.             {
  105.                 enqueue2(dequeue1());
  106.             }
  107.             cout<<dequeue1()<<endl;
  108.         }
  109.         else if (front1 == NULL)
  110.         {
  111.             while (front2->next2 != NULL)
  112.             {
  113.                 enqueue1(dequeue2());
  114.             }
  115.             cout<<dequeue2()<<endl;
  116.         }
  117.     }
  118.     getch();
  119. }

advertisement
 
Output
 
Enter the number of elements to be entered into stack
7
enter the element to be entered
3
enter the element to be entered
9
enter the element to be entered
1
enter the element to be entered
0
enter the element to be entered
6
enter the element to be entered
4
enter the element to be entered
2
 
 
Elements popped
 
2
4
6
0
1
9
3

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

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.