Answer to Question #263943 in C for S\/|\|

Question #263943

WAP to implement a stack using one queue.


1
Expert's answer
2021-11-11T17:38:42-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS