-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter3-queue.cpp
68 lines (57 loc) · 801 Bytes
/
chapter3-queue.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
// My implementation of queue with two stacks.
#include <stack>
using namespace std;
class Queue {
public:
Queue() {}
void push(int val) {
s1.push(val);
}
void pop() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
}
int size() {
return s1.size() + s2.size();
}
bool empty() {
return size() == 0;
}
int front() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
int back() {
if (s1.empty()) {
while (!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
}
return s1.top();
}
~Queue() {
while (!s1.empty()) {
s1.pop();
}
while (!s2.empty()) {
s2.pop();
}
}
private:
stack<int> s1, s2;
};
int main()
{
return 0;
}