Given a Queue data structure that supports standard operations like enqueue() and dequeue(). The task is to implement a Stack data structure using only instances of Queue and Queue operations allowed on the instances.
Stack can be implemented using two queues. Let Stack to be implemented be ‘s’ and queues used to implement are ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways:
Below is the idea to solve the problem:
The idea is to keep newly entered element at the front of ‘q1’ so that pop operation dequeues from ‘q1’. ‘q2’ is used to put every new element in front of ‘q1’.
Below is the implementation of the above approach.
C++/* Program to implement a stack using
two queue */
#include <bits/stdc++.h>
using namespace std;
class Stack {
// Two inbuilt queues
queue<int> q1, q2;
public:
void push(int x)
{
// Push x first in empty q2
q2.push(x);
// Push all the remaining
// elements in q1 to q2.
while (!q1.empty()) {
q2.push(q1.front());
q1.pop();
}
// swap the names of two queues
queue<int> q = q1;
q1 = q2;
q2 = q;
}
void pop()
{
// if no elements are there in q1
if (q1.empty())
return;
q1.pop();
}
int top()
{
if (q1.empty())
return -1;
return q1.front();
}
int size() { return q1.size(); }
};
// Driver code
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "current size: " << s.size() << endl;
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
cout << "current size: " << s.size() << endl;
return 0;
}
Output
current size: 3
3
2
1
current size: 1
Time Complexity
Auxiliary Space: O(N), As we use two queues for the implementation of a Stack.
Below is the idea to solve the problem:
The new element is always enqueued to q1. In pop() operation, if q2 is empty then all the elements except the last, are moved to q2. Finally, the last element is dequeued from q1 and returned.
Below is the implementation of the above approach:
C++
// Program to implement a stack
// using two queue
#include <bits/stdc++.h>
using namespace std;
class Stack {
queue<int> q1, q2;
public:
void pop()
{
if (q1.empty())
return;
// Leave one element in q1 and
// push others in q2.
while (q1.size() != 1) {
q2.push(q1.front());
q1.pop();
}
// Pop the only left element
// from q1
q1.pop();
// swap the names of two queues
queue<int> q = q1;
q1 = q2;
q2 = q;
}
void push(int x) { q1.push(x); }
int top()
{
if (q1.empty())
return -1;
while (q1.size() != 1) {
q2.push(q1.front());
q1.pop();
}
// last pushed element
int temp = q1.front();
// to empty the auxiliary queue after
// last operation
q1.pop();
// push last element to q2
q2.push(temp);
// swap the two queues names
queue<int> q = q1;
q1 = q2;
q2 = q;
return temp;
}
int size() { return q1.size(); }
};
// Driver code
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
cout << "current size: " << s.size() << endl;
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
cout << "current size: " << s.size() << endl;
return 0;
Output
current size: 3
3
2
1
current size: 1
Time Complexity
Auxiliary Space: O(N) since 2 queues are used.
Below is the idea to solve the problem:
Using only one queue and make the queue act as a Stack in modified way of the above discussed approach.
Follow the below steps to implement the idea
Below is the implementation for the above approach
C++#include <bits/stdc++.h>
using namespace std;
// Stack Class that acts as a queue
class Stack {
queue<int> q;
public:
void push(int data);
void pop();
int top();
int size();
bool empty();
};
// Push operation
void Stack::push(int data)
{
// Get previous size of queue
int s = q.size();
// Push the current element
q.push(data);
// Pop all the previous elements and put them after
// current element
for (int i = 0; i < s; i++) {
// Add the front element again
q.push(q.front());
// Delete front element
q.pop();
}
}
// Removes the top element
void Stack::pop()
{
if (q.empty())
cout << "No elements\n";
else
q.pop();
}
// Returns top of stack
int Stack::top() { return (q.empty()) ? -1 : q.front(); }
// Returns true if Stack is empty else false
bool Stack::empty() { return (q.empty()); }
int Stack::size() { return q.size(); }
int main()
{
Stack st;
st.push(1);
st.push(2);
st.push(3);
cout << "current size: " << st.size() << "\n";
cout << st.top() << "\n";
st.pop();
cout << st.top() << "\n";
st.pop();
cout << st.top() << "\n";
cout << "current size: " << st.size();
return 0;
}
Output
current size: 3
3
2
1
current size: 1
Time Complexity
Auxiliary Space: O(N) since 1 queue is used.
119 docs|30 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|