-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsolve.cpp
More file actions
43 lines (43 loc) · 725 Bytes
/
solve.cpp
File metadata and controls
43 lines (43 loc) · 725 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
42
43
#include <stack>
#include <iostream>
using namespace std;
class Queue {
private:
stack<int> first;
stack<int> second;
public:
bool empty(void) {
return first.empty() && second.empty();
}
void push(int x) {
first.push(x);
}
int peek(void) {
if (second.empty()) {
channel(first, second);
}
return second.top();
}
void pop(void) {
if (second.empty()) {
channel(first, second);
}
second.pop();
}
private:
void channel(stack<int> &src, stack<int> &dest) {
while (!src.empty()) {
dest.push(src.top());
src.pop();
}
}
};
int main(int argc, char **argv)
{
Queue queue;
queue.push(1);
queue.push(2);
queue.pop();
cout << queue.peek() << endl;
return 0;
}