The problem is opposite of this post. We are given a stack data structure with push and pop operations, the task is to implement a queue using instances of stack data structure and operations on them.

Queue-Data Structure- Implement Queue using Stacks

A queue can be implemented using two stacks. Let queue to be implemented be q and stacks used to implement q be stack1 and stack2. q can be implemented in two ways

[ad type=”banner”]

Method 1 (By making enQueue operation costly) This method makes sure that oldest entered element is always at the top of stack 1, so that deQueue operation just pops from stack1. To put the element at top of stack1, stack2 is used.

enQueue(q, x)
  1) While stack1 is not empty, push everything from satck1 to stack2.
  2) Push x to stack1 (assuming size of stacks is unlimited).
  3) Push everything back to stack1.

dnQueue(q)
  1) If stack1 is empty then error
  2) Pop an item from stack1 and return it

Method 2 (By making deQueue operation costly)In this method, in en-queue operation, the new element is entered at the top of stack1. In de-queue operation, if stack2 is empty then all the elements are moved to stack2 and finally top of stack2 is returned.

enQueue(q, x) 1) Push x to stack1 (assuming size of stacks is unlimited). deQueue(q) 1) If both stacks are empty then error. 2) If stack2 is empty While stack1 is not empty, push everything from stack1 to stack2. 3) Pop the element from stack2 and return it.

Method 2 is definitely better than method 1.
Method 1 moves all the elements twice in enQueue operation, while method 2 (in deQueue operation) moves the elements once and moves elements only if stack2 empty.

[ad type=”banner”] Implementation of method 2:

C Programming 

[pastacode lang=”c” manual=”%2F*%20Program%20to%20implement%20a%20queue%20using%20two%20stacks%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20structure%20of%20a%20stack%20node%20*%2F%0Astruct%20sNode%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20sNode%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20push%20an%20item%20to%20stack*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20int%20new_data)%3B%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Aint%20pop(struct%20sNode**%20top_ref)%3B%0A%20%0A%2F*%20structure%20of%20queue%20having%20two%20stacks%20*%2F%0Astruct%20queue%0A%7B%0A%20%20%20%20struct%20sNode%20*stack1%3B%0A%20%20%20%20struct%20sNode%20*stack2%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20enqueue%20an%20item%20to%20queue%20*%2F%0Avoid%20enQueue(struct%20queue%20*q%2C%20int%20x)%0A%7B%0A%20%20%20%20push(%26q-%3Estack1%2C%20x)%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20dequeue%20an%20item%20from%20queue%20*%2F%0Aint%20deQueue(struct%20queue%20*q)%0A%7B%0A%20%20%20%20int%20x%3B%0A%20%20%20%20%2F*%20If%20both%20stacks%20are%20empty%20then%20error%20*%2F%0A%20%20%20%20if(q-%3Estack1%20%3D%3D%20NULL%20%26%26%20q-%3Estack2%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Q%20is%20empty%22)%3B%0A%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%7D%0A%20%0A%2F*%20Move%20elements%20from%20satck1%20to%20stack%202%20only%20if%0Astack2%20is%20empty%20*%2F%0Aif(q-%3Estack2%20%3D%3D%20NULL)%0A%7B%0A%20%20%20%20while(q-%3Estack1%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20x%20%3D%20pop(%26q-%3Estack1)%3B%0A%20%20%20%20%20%20%20%20push(%26q-%3Estack2%2C%20x)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D%0A%20%0Ax%20%3D%20pop(%26q-%3Estack2)%3B%0Areturn%20x%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20push%20an%20item%20to%20stack*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20sNode*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20(struct%20sNode*)%20malloc(sizeof(struct%20sNode))%3B%0A%20%20%20%20%20%20%20%20if(new_node%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22Stack%20overflow%20%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%2F*%20put%20in%20the%20data%20*%2F%0Anew_node-%3Edata%20%3D%20new_data%3B%0A%20%0A%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0Anew_node-%3Enext%20%3D%20(*top_ref)%3B%0A%20%0A%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A(*top_ref)%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Aint%20pop(struct%20sNode**%20top_ref)%0A%7B%0A%20%20%20%20int%20res%3B%0A%20%20%20%20struct%20sNode%20*top%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*If%20stack%20is%20empty%20then%20error%20*%2F%0A%20%20%20%20if(*top_ref%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Stack%20overflow%20%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20top%20%3D%20*top_ref%3B%0A%20%20%20%20%20%20%20%20res%20%3D%20top-%3Edata%3B%0A%20%20%20%20%20%20%20%20*top_ref%20%3D%20top-%3Enext%3B%0A%20%20%20%20%20%20%20%20free(top)%3B%0A%20%20%20%20%20%20%20%20return%20res%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20function%20to%20test%20anove%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Create%20a%20queue%20with%20items%201%202%203*%2F%0A%20%20%20%20struct%20queue%20*q%20%3D%20(struct%20queue*)malloc(sizeof(struct%20queue))%3B%0A%20%20%20%20q-%3Estack1%20%3D%20NULL%3B%0A%20%20%20%20q-%3Estack2%20%3D%20NULL%3B%0A%20%20%20%20enQueue(q%2C%201)%3B%0A%20%20%20%20enQueue(q%2C%202)%3B%0A%20%20%20%20enQueue(q%2C%203)%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20Dequeue%20items%20*%2F%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%0Agetchar()%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output:

1 2 3

Queue can also be implemented using one user stack and one Function Call Stack. Below is modified Method 2 where recursion (or Function Call Stack) is used to implement queue using only one user defined stack.

enQueue(x)
  1) Push x to stack1.

deQueue:
  1) If stack1 is empty then error.
  2) If stack1 has only one element then return it.
  3) Recursively pop everything from the stack1, store the popped item 
    in a variable res,  push the res back to stack1 and return res

The step 3 makes sure that the last popped item is always returned and since the recursion stops when there is only one item in stack1 (step 2), we get the last element of stack1 in dequeue() and all other items are pushed back in step.

[ad type=”banner”]

Implementation of method 2 using Function Call Stack

[pastacode lang=”c” manual=”%2F*%20Program%20to%20implement%20a%20queue%20using%20one%20user%20defined%20stack%20%0Aand%20one%20Function%20Call%20Stack%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20structure%20of%20a%20stack%20node%20*%2F%0Astruct%20sNode%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20sNode%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20structure%20of%20queue%20having%20two%20stacks%20*%2F%0Astruct%20queue%0A%7B%0A%20%20%20%20struct%20sNode%20*stack1%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20push%20an%20item%20to%20stack*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20int%20new_data)%3B%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Aint%20pop(struct%20sNode**%20top_ref)%3B%0A%20%0A%2F*%20Function%20to%20enqueue%20an%20item%20to%20queue%20*%2F%0Avoid%20enQueue(struct%20queue%20*q%2C%20int%20x)%0A%7B%0A%20%20%20%20push(%26q-%3Estack1%2C%20x)%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20dequeue%20an%20item%20from%20queue%20*%2F%0Aint%20deQueue(struct%20queue%20*q)%0A%7B%0A%20%20%20%20int%20x%2C%20res%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20If%20both%20stacks%20are%20empty%20then%20error%20*%2F%0A%20%20%20%20if(q-%3Estack1%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Q%20is%20empty%22)%3B%0A%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%20if(q-%3Estack1-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20pop(%26q-%3Estack1)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20pop%20an%20item%20from%20the%20stack1%20*%2F%0A%20%20%20%20%20%20%20%20x%20%3D%20pop(%26q-%3Estack1)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20store%20the%20last%20dequeued%20item%20*%2F%0A%20%20%20%20%20%20%20%20res%20%3D%20deQueue(q)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F*%20push%20everything%20back%20to%20stack1%20*%2F%0A%20%20%20%20%20%20%20%20push(%26q-%3Estack1%2C%20x)%3B%0A%20%20%20%20%20%20%20%20return%20res%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20push%20an%20item%20to%20stack*%2F%0Avoid%20push(struct%20sNode**%20top_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20sNode*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20(struct%20sNode*)%20malloc(sizeof(struct%20sNode))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20if(new_node%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Stack%20overflow%20%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%0A%2F*%20put%20in%20the%20data%20*%2F%0Anew_node-%3Edata%20%3D%20new_data%3B%0A%20%0A%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0Anew_node-%3Enext%20%3D%20(*top_ref)%3B%0A%20%0A%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A(*top_ref)%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20pop%20an%20item%20from%20stack*%2F%0Aint%20pop(struct%20sNode**%20top_ref)%0A%7B%0A%20%20%20%20int%20res%3B%0A%20%20%20%20struct%20sNode%20*top%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*If%20stack%20is%20empty%20then%20error%20*%2F%0A%20%20%20%20if(*top_ref%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Stack%20overflow%20%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20getchar()%3B%0A%20%20%20%20%20%20%20%20exit(0)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20top%20%3D%20*top_ref%3B%0A%20%20%20%20res%20%3D%20top-%3Edata%3B%0A%20%20%20%20*top_ref%20%3D%20top-%3Enext%3B%0A%20%20%20%20free(top)%3B%0A%20%20%20%20return%20res%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20function%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Create%20a%20queue%20with%20items%201%202%203*%2F%0A%20%20%20%20struct%20queue%20*q%20%3D%20(struct%20queue*)malloc(sizeof(struct%20queue))%3B%0A%20%20%20%20q-%3Estack1%20%3D%20NULL%3B%0A%20%20%20%20%20%0A%20%20%20%20enQueue(q%2C%201)%3B%0A%20%20%20%20enQueue(q%2C%202)%3B%0A%20%20%20%20enQueue(q%2C%203)%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F*%20Dequeue%20items%20*%2F%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20deQueue(q))%3B%0A%20%20%20%20%20%0Agetchar()%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

1 2 3
[ad type=”banner”]

 

 

 

 

 

 

 

Categorized in: