Introduction
Have you ever wondered how computers keep track of things in the right order? Stacks and queues are two fundamental data structures that help us do just that. But what if we told you there's a sneaky way to build a stack using a queue?
In this blog, we'll discuss stacks and queues, and explore how they work and why they're important. Then, we'll unveil the surprising trick of implementing a stack using queues!

There are several approaches for the implementation of stacks using queues. We will see each of them one by one.
Recommended Topic, Binary to Hex Converter,C Static Function
Approach 1 -By making push() operation costly
In this method, we will use two queues for the implementation of stacks using queues.
The idea is to keep the last entered element at the front of the queue. Why? Because the stack is a Last in First out data structure while in the queue, the elements are removed from the front end. So, when we will do a pop operation, then the last entered element will be the first one to be removed as we will ensure that it is kept at the front of the queue.
Algorithm
- Push Operation –
To push a new element in the stack, move all the elements from the first queue to the second queue and then enqueue the new element into the first queue. Finally, move all the elements from the second queue back to the first queue.
This is done to ensure that the newly entered element lies at the front of the queue.
Time Complexity –
It is O(n), where n is the number of elements in the stack.
All the elements are dequeued from the first queue one by one and then enqueued into the second queue and again moved back to the first queue. So, if there are n elements in the first queue initially, then the series of operations performed on each of them are –
- dequeue from the first queue
- enqueue to the second queue
- dequeue from the second queue
- enqueue to the first queue
And we know that each enqueue/dequeue operation is O(1). So, total number of operations performed = n*(4*O(1)) + O(1) (to enqueue a new element), which is O(n).
Alternative Way:
- Enqueue new element to the second queue, say Q2
- Dequeue all the n elements from the first queue, say Q1, and enqueue them into Q2.
- Swap the queues Q1 and Q2 to avoid copying all the elements from Q2 to Q1.
- Pop Operation –
To pop an element from the stack, dequeue the element at the front of the first queue.
Time Complexity –
It is O(1) because we do only one dequeue operation.
Space Complexity – It is O(n) as we use two additional queues for the implementation of stack functions.
Let’s take an example to understand the implementation of stacks using queues easily-
Suppose we are given a series like this -
5, 7, 3, P
where P means the pop operation has to be performed and integer value means push operation.
Initially, we have two empty queues Q1 and Q2, like this -

Step 1: Enqueue 5 to Q1.

Step 2: Next, we have to enqueue 7 such that it remains at the front end of Q1.
Dequeue 5 from Q1 and enqueue it into Q2. And enqueue 7 to Q1.

Now, dequeue 5 from Q2 and enqueue it to Q1.

Step 3: Now, to enqueue 3, we will move 7 and 5 from Q1 to Q2 and enqueue 3 to Q1.

Now, move 7 and 5 from Q2 to Q1.

Step 4: Next, we have P in the series, meaning we need to pop from the stack.
To do this, simply perform a dequeue operation on Q1, which will remove 3.

You can also read about dynamic array in c and Short int in C Programming
C++ Implementation
Output:
Pushed: 5
Pushed: 7
Pushed: 31
Pushed: 4
Pushed: 2
Popped: 2
Popped: 4
Popped: 31
Popped: 7
Popped: 5
Stack Underflow
Click on the following link to read further: Features of C Language