-
Notifications
You must be signed in to change notification settings - Fork 0
/
225_impStakcWithQueue.cpp
executable file
·69 lines (57 loc) · 1.19 KB
/
225_impStakcWithQueue.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
#include <iostream>
#include <queue>
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
send2Store();
inOut.push(x);
sendBack2InOut();
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int ret = inOut.front();
inOut.pop();
return ret;
}
/** Get the top element. */
int top() {
return inOut.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return inOut.empty();
}
private:
void send2Store() {
while (0 != inOut.size()) {
store.push(inOut.front());
inOut.pop();
}
}
void sendBack2InOut() {
while (0 != store.size()) {
inOut.push(store.front());
store.pop();
}
}
std::queue<int> inOut;
std::queue<int> store;
};
int
main(int argc, char const *argv[])
{
MyStack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
while (!stack.empty()) {
std::cout << stack.pop() << std::endl;
}
return 0;
}