-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path232.cpp
More file actions
41 lines (36 loc) · 900 Bytes
/
Copy path232.cpp
File metadata and controls
41 lines (36 loc) · 900 Bytes
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
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
while (!utility.empty()) {
int temp = utility.top();
utility.pop();
reserve.push(temp);
}
utility.push(x);
while (!reserve.empty()) {
int temp = reserve.top();
reserve.pop();
utility.push(temp);
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int temp = utility.top();
utility.pop();
return temp;
}
/** Get the front element. */
int peek() {
return utility.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return utility.empty();
}
private:
stack<int> utility, reserve;
};