-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy path3-5.cpp
66 lines (56 loc) · 1007 Bytes
/
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
63
#include <iostream>
#include <stack>
using namespace std;
template <class T>
class MyQueue{
stack<T> s_new;
stack<T> s_old;
public:
void push(T i){
s_new.push(i);
}
void pop(){
if (!s_old.empty()){
s_old.pop();
}else{
if (s_new.empty()){
cerr<<"Empty Queue!" << endl;
}else{
while (!s_new.empty()){
s_old.push(s_new.top());
s_new.pop();
}
s_old.pop();
}
}
}
T front(){
if (!s_old.empty()){
return s_old.top();
}else{
if (s_new.empty()){
cerr<<"Empty Queue!" << endl;
}else{
while (!s_new.empty()){
s_old.push(s_new.top());
s_new.pop();
}
return s_old.top();
}
}
}
bool empty(){
return s_new.empty()&&s_old.empty();
}
};
int main(){
MyQueue<int> mq;
for (int i=0;i<10;i++){
mq.push(i);
}
while (!mq.empty()) {
cout<< mq.front()<<endl;
mq.pop();
}
return 0;
}