C Program to Implement Queues using Stack

This is a C Program to Implement Queues using Stacks.

Problem Description

This program implements queue using stack.

Problem Solution

1. Take the elements as input and store it in the stack array. Use this array to show the stack operations.
2. Transfer the elements from the stack array into the new array. Do the queue operations in the new array.
3. Exit.

Program/Source Code

Here is source code of the C Program to Implement Queues using Stacks. 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 Queues using Stacks
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void push1(int);
  8. void push2(int);
  9. int pop1();
  10. int pop2();
  11. void enqueue();
  12. void dequeue();
  13. void display();
  14. void create();
  15.  
  16. int st1[100], st2[100];
  17. int top1 = -1, top2 = -1;
  18. int count = 0;
  19.  
  20. void main()
  21. {
  22.     int ch;
  23.  
  24.     printf("\n1 - Enqueue element into queue");
  25.     printf("\n2 - Dequeu element from queue");
  26.     printf("\n3 - Display from queue");
  27.     printf("\n4 - Exit");
  28.     create();
  29.     while (1)
  30.     {
  31.         printf("\nEnter choice");
  32.         scanf("%d", &ch);
  33.         switch (ch)
  34.         {
  35.         case 1:
  36.             enqueue();
  37.             break;
  38.         case 2:
  39.             dequeue();
  40.             break;
  41.         case 3:
  42.             display();
  43.             break;
  44.         case 4:
  45.             exit(0);
  46.         default:
  47.             printf("Wrong choice");
  48.         }
  49.     }
  50. }
  51.  
  52. /*Function to create a queue*/
  53. void create()
  54. {
  55.     top1 = top2 = -1;
  56. }
  57.  
  58. /*Function to push the element on to the stack*/
  59. void push1(int data)
  60. {
  61.     st1[++top1] = data;
  62. }
  63.  
  64. /*Function to pop the element from the stack*/
  65. int pop1()
  66. {
  67.     return(st1[top1--]);
  68. }
  69.  
  70. /*Function to push an element on to stack*/
  71. void push2(int data)
  72. {
  73.     st2[++top2] = data;
  74. }
  75.  
  76. /*Function to pop an element from th stack*/
  77.  
  78. int pop2()
  79. {
  80.     return(st2[top2--]);
  81. }
  82.  
  83. /*Function to add an element into the queue using stack*/
  84. void enqueue()
  85. {
  86.     int data, i;
  87.  
  88.     printf("Enter data into queue");
  89.     scanf("%d", &data);
  90.     push1(data);
  91.     count++;
  92. }
  93.  
  94. /*Function to delete an element from the queue using stack*/
  95.  
  96. void dequeue()
  97. {
  98.     int i;
  99.  
  100.     for (i = 0;i <= count;i++)
  101.     {
  102.         push2(pop1());
  103.     }
  104.     pop2();
  105.     count--;
  106.     for (i = 0;i <= count;i++)
  107.     {
  108.         push1(pop2());
  109.     }
  110. }
  111.  
  112. /*Function to display the elements in the stack*/
  113.  
  114. void display()
  115. {
  116.     int i;
  117.  
  118.     for (i = 0;i <= top1;i++)
  119.     {
  120.         printf(" %d ", st1[i]);
  121.     }
  122. }
Program Explanation

1. Ask the user for the operations like insert, delete and display.
2. According to the entered option access the respective function using switch statement.
3. In the enqueue() function push the element into the array st1[].
4. In the dequeue () function, firstly transfer all the elements of the array st1[] into the new array st2[]. Then the pop the first element of the new array.
5. In the display() function print all the elements of the st1[].

advertisement
advertisement
Runtime Test Cases
1 - Enqueue element into queue
2 - Dequeu element from queue
3 - Display from queue
4 - Exit
Enter choice1
Enter data into queue10
 
Enter choice1
Enter data into queue20
 
Enter choice1
Enter data into queue30
 
Enter choice1
Enter data into queue40
 
Enter choice3
 10  20  30  40
Enter choice2
 
Enter choice3
 20  30  40
Enter choice4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.