-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_5.cpp
62 lines (58 loc) · 1.08 KB
/
3_5.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
57
58
59
60
61
62
//
// 3_5.cpp
//
//
// Created by Pengyan Qin on 6/24/15.
//
//
#include <stack>
#include <iostream>
#include <cstdlib>
using namespace std;
//a queue cut into 2 pieces, s1 is the in, s2 is out
class MyQueue{
stack<int> s1;
stack<int> s2;
public:
bool empty(){
return s1.empty() && s2.empty();
}
void push(int val){
s1.push(val);
}
void pop(){
if(empty())
return;
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
}
int& top(){
if(empty())
exit(0);
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
int size(){
return s1.size() + s2.size();
}
};
int main(){
MyQueue s;
for(int i = 0; i < 4; ++i)
s.push(i); // 0, 1, 2, 3
s.pop();
s.pop(); // 2, 3
cout << s.top() << endl;
cout << s.size() << endl;
cout << s.empty() << endl;
}