forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAC_front_back_stack_1.cpp
57 lines (46 loc) · 1.05 KB
/
AC_front_back_stack_1.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_front_back_stack_1.cpp
* Create Date: 2015-07-27 09:41:49
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Queue {
private:
stack<int> stack_front;
stack<int> stack_back;
void back_to_front() {
while (!stack_back.empty()) {
stack_front.push(stack_back.top());
stack_back.pop();
}
}
public:
// Push element x to the back of queue.
void push(int x) {
stack_back.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (stack_front.empty()) {
back_to_front();
}
return stack_front.pop();
}
// Get the front element.
int peek(void) {
if (stack_front.empty()) {
back_to_front();
}
return stack_front.top();
}
// Return whether the queue is empty.
bool empty(void) {
return stack_front.empty() && stack_back.empty();
}
};
int main() {
return 0;
}