-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCQueue.cpp
More file actions
65 lines (63 loc) · 1.32 KB
/
CQueue.cpp
File metadata and controls
65 lines (63 loc) · 1.32 KB
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
/*
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入节点和在队列头部删除节点的功能。
template<typename T>
class CQueue{
public:
void appendTail(const T& node);
T deleteHead();
private:
stack<T> stack1;
stack<T> stack2;
};
*/
#include<iostream>
#include<stack>
using namespace std;
template<typename T>
class CQueue{
public:
void appendTail(const T& node);
T deleteHead();
private:
stack<T> stack1;
stack<T> stack2;
};
template<typename T>
void CQueue<T>::appendTail(const T& node)
{
if (stack1.empty())
{
stack1.push(node);
}
else{
while (!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
stack1.push(node);
while (!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
}
}
template<typename T>
T CQueue<T>::deleteHead()
{
T ret = stack1.top();
stack1.pop();
return ret;
}
int main(int argc, char const *argv[]) {
CQueue<int> cqi;
cqi.appendTail(1);
cqi.appendTail(2);
cqi.appendTail(3);
cout << cqi.deleteHead() << endl;
cout << cqi.deleteHead() << endl;
cout << cqi.deleteHead() << endl;
cin.get();
return 0;
}