-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_1.cpp
74 lines (64 loc) · 1 KB
/
7_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
//题目:使用栈模拟队列
template<typename T> class iQueue
{
public:
stack<int> s1;
stack<T> s2;
void ipush(T t); //进队列
T ifront(); //取出队列元素,但是不删除
void ipop(); //出队列
};
//进队列
template<typename T> void iQueue<T>::ipush(T t)
{
s1.push(t);
}
//取出队列元素,但是不删除
template<typename T> T iQueue<T>::ifront()
{
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
if(s2.empty()){
throw new exception("empty queue!!");
}
T t = s2.top();
return t;
}
//出队列
template<typename T> void iQueue<T>::ipop()
{
if(s2.empty()){
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
}
if(s2.empty()){
throw new exception("empty queue!!");
}
s2.pop();
}
int main(){
iQueue<int> q;
q.ipush(1);
q.ipush(2);
q.ipush(3);
cout<<q.ifront()<<endl;;
q.ipop();
cout<<q.ifront()<<endl;;
q.ipop();
cout<<q.ifront()<<endl;;
q.ipop();
cout<<q.ifront()<<endl;;
return 0;
}