-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCQueue.h
More file actions
62 lines (52 loc) · 792 Bytes
/
CQueue.h
File metadata and controls
62 lines (52 loc) · 792 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<stack>
using namespace std;
//Á½¸öջʵÏÖ¶ÓÁÐ
template<class T>
class CQueue
{
public:
void AppendTail(const T &node)
{
_stackIn.push(node);
}
T DeleteHead()
{
if (_stackOut.empty())
{
if (_stackIn.empty())
{
return T();
}
while (!(_stackIn.empty()))
{
_stackOut.push(_stackIn.top());
_stackIn.pop();
}
}
if (_stackOut.empty())
{
return T();
}
T tmp = _stackOut.top();
_stackOut.pop();
return tmp;
}
protected:
stack<T> _stackIn;
stack<T> _stackOut;
};
void TestCQueue()
{
CQueue<int> cq;
cq.AppendTail(1);
cq.DeleteHead();
cq.DeleteHead();
cq.AppendTail(2);
cq.AppendTail(3);
cq.DeleteHead();
cq.AppendTail(4);
cq.AppendTail(5);
cq.DeleteHead();
}