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