How to implement stack using vector and queue in C++

1 Answer

0 votes
#include <iostream>
#include <queue>

using namespace std;

class Stack
{
	queue<int> q;

public:
	void push(int number) {
		q.push(number);
	}

	int pop() {
	    if (q.empty()) {
			cout << "empty";
			exit(0);
		}

		int front = q.front();
		q.pop();

		return front;
	}
};

int main()
{
	vector<int> vec = {9, 4, 0, 1, 7};

	Stack s;
	
	for (int number : vec) {
		s.push(number);
	}

	for (int i = 0; i <= vec.size(); i++)
		cout << s.pop() << endl;

	return 0;
}



/*
run:

9
4
0
1
7
empty

*/

 



answered Apr 24, 2019 by avibootz

Related questions

2 answers 106 views
1 answer 218 views
218 views asked Mar 31, 2018 by avibootz
1 answer 263 views
1 answer 161 views
1 answer 85 views
...