-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkQueue.cpp
92 lines (91 loc) · 2.02 KB
/
linkQueue.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include"error.cpp"
class error;
template<class T>
class linkQueue
{
private:
struct Node
{
T data;
Node *next;
Node(T x,Node *p=NULL):data(x),next(p){}
Node():next(NULL){}
};
Node* head;
Node* rear;
public:
linkQueue():head(NULL),rear(NULL){}
~linkQueue()
{
Node *tmp;
while(head)
{
tmp=head;
head=head->next;
delete tmp;
}
}
bool isEmpty()
{
return head==NULL;
}
void enQueue(const T& x)
{
if(!head){
head=rear=new Node(x);
return;
}
Node *tmp=new Node(x);
rear->next=tmp;
rear=rear->next;
}
T deQueue()
{
if(!head)
{
throw error("deQueue error, quque is empty");
}
T x=head->data;
Node* tmp=head;
head=head->next;
if(!head)
head=rear=NULL;
delete tmp;
return x;
}
T getRear()
{
if(!rear)
throw error("getHead error, quque is empty");
return rear->data;
}
T getHead()
{
if(!head)
throw error("getHead error, quque is empty");
return head->data;
}
};
// int main()
// {
// linkQueue<int> q;
// for(int i=0;i<30;++i)
// {
// q.enQueue(i);
// std::cout<<q.getRear()<<' ';
// }
// try
// {
// std::cout<<std::endl;
// for(int i=0;i<100;i++)
// {
// q.deQueue();
// std::cout<<q.getHead();
// }
// }
// catch(error & e)
// {
// std::cerr << e.what() << '\n';
// }
// }