Skip to content

Commit ac71c21

Browse files
author
whd
committed
upload
1 parent 891f5d9 commit ac71c21

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Stack {
2+
public:
3+
// Push element x onto stack.
4+
void push(int x) {
5+
while (!q.empty()) {
6+
tmp.push(q.front());
7+
q.pop();
8+
}
9+
q.push(x);
10+
while (!tmp.empty()) {
11+
q.push(tmp.front());
12+
tmp.pop();
13+
}
14+
}
15+
16+
// Removes the element on top of the stack.
17+
void pop() {
18+
q.pop();
19+
}
20+
21+
// Get the top element.
22+
int top() {
23+
return q.front();
24+
}
25+
26+
// Return whether the stack is empty.
27+
bool empty() {
28+
return q.empty();
29+
}
30+
private:
31+
queue<int> q, tmp;
32+
};

0 commit comments

Comments
 (0)