Implementation of Queue using Stack

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 three ways: 

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): 

  • While stack1 is not empty, push everything from stack1 to stack2.
  • Push x to stack1 (assuming size of stacks is unlimited).
  • Push everything back to stack1.
  • Time complexity will be O(N).

deQueue(q): 

  • If stack1 is empty, then error
  • Pop an item from stack1 and return it.
  • Time complexity will be O(1).

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):

  • Push x to stack1 (assuming size of stacks is unlimited).
  • Time complexity will be O(1)

deQueue(q):

  • If both stacks are empty then error.
  • If stack2 is empty, while stack1 is not empty, push everything from stack1 to stack2.
  • Pop the element from stack2 and return it.
  • Time complexity will be O(N)

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. So, the amortized complexity of the dequeue operation becomes Θ(1)

Method 3 (Using one user stack and one function call stack):  Method 2 can be modified where recursion (or Function Call Stack) is used to implement queue using only one user defined stack.

enQueue(x):

  • Push x to stack1.
  • Time complexity will be O(1)

deQueue():

  • If stack1 is empty, then error.
  • If stack1 has only one element, then return it.
  • Recursively pop everything from the stack1, store the popped item in variable res, push the res back to stack1 and return res.
  • Time Complexity will be O(N).

Queue Notes for GATE Exam [2024]

A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. Queue is a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.  The element, which is first pushed into the order, the operation is first performed on that.

FIFO Property in Queue

Table of Content

  • Implementing Queues using Arrays:
  • Implementing Queues using Linked List:
  • Implementation of Queue using Stack:
  • Circular Queue:
  • Priority Queue:
  • Double Ended Queue:
  • GATE Archives – Previous Years Questions on Queue:

Similar Reads

Implementing Queues using Arrays:

To implement a queue using an array,...

Implementing Queues using Linked List:

To implement a queue using a Linked List,...

Implementation of Queue using Stack:

...

Circular Queue:

A Circular Queue is an extended version of a normal queue where the last element of the queue is connected to the first element of the queue forming a circle. The operations are performed based on FIFO (First In First Out) principle. It is also called ‘Ring Buffer’....

Priority Queue:

A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved before elements with lower priority values.If we add an element with a high priority value to a priority queue, it may be inserted near the front of the queue, while an element with a low priority value may be inserted near the back....

Double Ended Queue:

Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends....

GATE Archives – Previous Years Questions on Queue:

Q1. Following is C like pseudo-code of a function that takes a Queue as an argument, and uses a stack S to do processing....

Contact Us