-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkQueue.cpp
More file actions
86 lines (86 loc) · 1.41 KB
/
LinkQueue.cpp
File metadata and controls
86 lines (86 loc) · 1.41 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data;
struct Node<T>*next;
};
template<class T>
class LinkQueue
{
public:
LinkQueue()
{
front = rear = new Node<T>;
front->next = NULL;
}
~LinkQueue();
void Enqueue(T x);
T Dequeue();
T GetFront();
T Getrear();
bool Empty() {
return front == rear ? true : false;
}
private:
Node <T> *front;
Node <T> *rear;
};
template<class T>
T LinkQueue<T>::Getrear()
{
if (!(rear->data)) throw "underflow";
return rear->data;
}
template <class T>
void LinkQueue<T>::Enqueue(T x)
{
rear->next = new Node<T>;
rear = rear->next;
rear->data = x;
rear->next = NULL;
}
template <class T>
T LinkQueue<T>::Dequeue()
{
Node<T>*p = front->next;
if (!p)throw"ÏÂÒç";
front->next = p->next;
T x = p->data;
delete p;
if (!(front->next)) rear = front;
return x;
}
template <class T>
T LinkQueue<T>::GetFront()
{
if (!(front->next)) throw"ÉÏÒç";
return front->next->data;
}
template <class T>
LinkQueue<T>::~LinkQueue()
{
while (front)
{
rear = front->next;
delete front;
front = rear;
}
}
//void main()
//{
// LinkQueue<int> q;
// int m[5] = { 1,2,3,4,5 };
// for (int i = 0;i <5;i++)
// {
// q.Enqueue(m[i]);
// cout << q.Getrear() << " ";
// }
// cout << q.Empty() << endl;
// for (int i = 0;i <5;i++)
// {
// q.Dequeue();
// }
// cout << q.Empty() << endl;
//}