forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Efficient_Dequeue.cpp
72 lines (58 loc) · 1.25 KB
/
Efficient_Dequeue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
class Stack {
private:
queue<int> q;
public:
// Push element x onto stack.
void push(int x) {
unsigned int size = q.size();
this->q.push(x);
while (size--){
q.push(q.front());
q.pop();
}
}
// Removes the element on top of the stack.
void pop() {
q.pop();
}
// Get the top element.
int top() {
return q.front();
}
// Return whether the stack is empty.
bool empty() {
return q.empty();
}
void printStack() {
if (q.empty()) {
cout << "stack is empty!\n";
return;
}
queue<int> temp;
temp = q;
while (temp.size()) {
cout << temp.front() << endl;
temp.pop();
}
}
};
int main() {
Stack pop_efficient;
pop_efficient.push(10);
pop_efficient.push(20);
pop_efficient.push(30);
pop_efficient.push(40);
pop_efficient.push(50);
pop_efficient.pop();
pop_efficient.push(60);
pop_efficient.push(70);
pop_efficient.pop();
pop_efficient.push(80);
pop_efficient.push(90);
pop_efficient.printStack();
return 0;
}