This repository was archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_priority.h
118 lines (113 loc) · 1.85 KB
/
queue_priority.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
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T value;
unsigned int priority;
Node *Next;
};
template <typename T>
class queue_priority
{
Node<T> *Head;
public:
queue_priority();
void push(T, int);
bool pop(T &);
bool pop_priority(int &);
int size();
bool isEmpty();
void print();
~queue_priority();
};
template <typename T>
queue_priority<T>::queue_priority()
{
Head = new (Node<T>);
Head->Next = nullptr;
}
template <typename T>
queue_priority<T>::~queue_priority()
{
Node<T> *t = Head->Next, *p;
delete Head;
while(t != nullptr)
{
p = t;
t = t->Next;
delete p;
}
}
template <typename T>
void queue_priority<T>::push(T item, int prio)
{
Node<T> *t = new (Node<T>);
t->value = item;
t->priority = prio;
Node<T> *p = Head;
while (p->Next != nullptr && (p->Next)->priority <= prio)
p = p->Next;
t->Next = p->Next;
p->Next = t;
}
template <typename T>
bool queue_priority<T>::pop(T &out)
{
if (isEmpty())
return false;
Node<T> *t = Head->Next;
out = t->value;
Head->Next = t->Next;
delete t;
return true;
}
template <typename T>
bool queue_priority<T>::pop_priority(int &out)
{
if (isEmpty())
return false;
Node<T> *t = Head->Next;
out = t->priority;
return true;
}
template <typename T>
bool queue_priority<T>::isEmpty()
{
if (Head->Next == nullptr)
return true;
return false;
}
template <typename T>
int queue_priority<T>::size()
{
Node<T> *t = Head->Next;
int count = 0;
while (t != nullptr)
{
t = t->Next;
count++;
}
return count;
}
template <typename T>
void queue_priority<T>::print()
{
Node<T> *t = Head->Next;
while (t != nullptr)
{
cout << t->value << " ";
t = t->Next;
}
cout << endl;
}
// int main(int argc, char const *argv[])
// {
// queue_priority<char> a;
// a.push('f', 5);
// a.push('a', 5);
// a.push('c', 5);
// a.push('k', 5);
// a.print();
// return 0;
// }