A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where the last element pushed into the stack is the first one to be popped out. Although a stack can be easily implemented directly using an array or a linked list, we can also implement a stack using two queues.
What is a Queue?
A queue is another common data structure that follows the First-In-First-Out (FIFO) principle, where the first element enqueued is the first one to be dequeued. A queue can be implemented using an array or a linked list.
Using Two Queues to Implement a Stack
To implement a stack using two queues, we need to maintain two queues: queue1 and queue2. The basic idea is to use one queue for storing elements and the other as a temporary queue to support stack operations.
Here’s the implementation of a stack using two queues in C++:
#include <iostream>
#include <queue>
using namespace std;
class Stack {
private:
queue<int> primaryQueue;
queue<int> secondaryQueue;
public:
void push(int val) {
secondaryQueue.push(val);
while (!primaryQueue.empty()) {
secondaryQueue.push(primaryQueue.front());
primaryQueue.pop();
}
swap(primaryQueue, secondaryQueue);
}
void pop() {
if (!primaryQueue.empty()) {
primaryQueue.pop();
}
}
int top() {
if (!primaryQueue.empty()) {
return primaryQueue.front();
}
return -1; // return -1 if stack is empty
}
bool empty() {
return primaryQueue.empty();
}
};
int main() {
Stack s;
s.push(5);
s.push(10);
s.push(15);
cout << "Top element: " << s.top() << endl;
s.pop();
cout << "Top element after pop: " << s.top() << endl;
s.push(20);
cout << "Is stack empty? " << (s.empty() ? "Yes" : "No") << endl;
return 0;
}
Explanation
-
Two queues (
primaryQueueandsecondaryQueue) are declared as private members of theStackclass. -
The
pushoperation is performed by pushing the element into thesecondaryQueue. Then, all the elements from theprimaryQueueare transferred to thesecondaryQueueone by one. Finally, theprimaryQueueandsecondaryQueueare swapped. -
The
popoperation is simply performed by popping the front element of theprimaryQueue. -
The
topoperation returns the front element of theprimaryQueueif it is not empty, otherwise it returns -1 to indicate an empty stack. -
The
emptyoperation checks if theprimaryQueueis empty or not and returns a boolean value accordingly.
Conclusion
In this blog post, we have covered the implementation of a stack using two queues in C++. Using this approach, we can easily implement stack operations like push, pop, top, and check if the stack is empty. This implementation provides an alternative way to implement stacks and showcases the versatility of queues in data structures.
#stacks #queues