-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueue.h
103 lines (97 loc) · 1.27 KB
/
Queue.h
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
93
94
95
96
97
98
99
100
101
102
#pragma once
#include"Node.h"
#include <iostream>
using namespace std;
template<class Type>
class Queue
{
private:
Node<Type>* front;
int count;
Node<Type>* rear;
public:
Queue()
{
front = nullptr;
rear = nullptr;
count = 0;
}
bool EnQueue(Type item)
{
Node<Type>* temp;
temp = new Node<Type>;
temp->set_item(item);
if (QueueEmpty())
front = temp;
else
rear->set_next(temp);
rear = temp;
count++;
return true;
}
bool DeQueue(Type& x)
{
if (count == 0)
return false;
Node<Type>* delptr;
delptr = front;
if (count == 1)
{
rear = NULL;
front = NULL;
}
else
{
front = front->get_next();
}
delptr->set_next(nullptr);
x = delptr->get_item();
count--;
return true;
}
bool QueueEmpty()
{
return !count;
}
void DistroyQueue()
{
Type x;
int c = count;
for (int i = 0; i < c; i++)
DeQueue(x);
}
Node<Type>* GetFront()
{
return front;
}
Node<Type>* GetRear()
{
return rear;
}
int GetCount()
{
return count;
}
Type Peek()
{
if (!QueueEmpty())
return front->get_item();
else
return NULL;
}
~Queue()
{
DistroyQueue();
}
void print()
{
Node<Type>* temp = front;
while (temp)
{
cout << temp->get_item();
temp = temp->get_next();
if (temp)
cout << ',';
}
}
};