WAP to implement a stack using one queue.
#include<bits/stdc++.h>
using namespace std;
class Stack
{
queue<int>q;
public:
void push(int d)
{
int st = q.size();
q.push(d);
for (int i=0; i<st; i++)
{
q.push(q.front());
q.pop();
}
}
void pop()
{
if (q.empty())
cout << "Empty\n";
else
q.pop();
}
int top()
{
return (q.empty())? -1 : q.front();
}
bool empty()
{
return (q.empty());
}
};
int main()
{
Stack s;
s.push(30);
s.push(47);
return 0;
}
Comments
Leave a comment