-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPriQueue.h
127 lines (118 loc) · 1.79 KB
/
PriQueue.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @file PriQueue.h
* @brief PriQueue Data structure
* @version 0.1
* @copyright Copyright secured by YMY Team(c) 2022
*/
#pragma once
#include "PriNode.h"
#include<iostream>
template<class Type>
class PriQueue
{
private:
PriNode<Type>* front;
int count;
PriNode<Type>* rear;
public:
PriQueue()
{
front = nullptr;
rear = nullptr;
count = 0;
}
bool EnQueue(Type item, float priority = 0)
{
PriNode<Type>* temp;
PriNode<Type>* cur = front;
temp = new PriNode<Type>;
temp->set_item(item);
temp->set_priority(priority);
if (QueueEmpty())
{
front = temp;
rear = temp;
}
else if (front->get_priority() < priority)
{
temp->set_next(front);
front = temp;
}
else
{
while (cur->get_next() != NULL && cur->get_next()->get_priority() >= priority)
cur = cur->get_next();
if (!cur->get_next())
rear = temp;
temp->set_next(cur->get_next());
cur->set_next(temp);
}
count++;
return true;
}
bool DeQueue(Type& x)
{
if (count == 0)
return false;
PriNode<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);
}
PriNode<Type>* GetFront()
{
return front;
}
PriNode<Type>* GetRear()
{
return rear;
}
int GetCount()
{
return count;
}
Type Peek()
{
if (!QueueEmpty())
return front->get_item();
else
return NULL;
}
~PriQueue()
{
DistroyQueue();
}
void print()
{
PriNode<Type>* temp = front;
while (temp)
{
cout << (temp->get_item());
temp = temp->get_next();
if (temp)
cout << ',';
}
}
};