-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.h
75 lines (52 loc) · 921 Bytes
/
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
#pragma once
template <class T> class Queue
{
private:
struct Elem {
T info;
Elem *sled;
Elem(T inf, Elem *pok = nullptr) : info(inf), sled(pok) {};
};
Elem *front, *rear;
public:
Queue() : front(nullptr), rear(nullptr) {};
friend bool EMPTY_Q(Queue & q)
{
if (q.front == nullptr) return true;
return false;
}
friend void INSERT(Queue & q, T br)
{
Elem *novi = new Elem(br);
if (q.front == nullptr) {
q.front = q.rear = novi;
}
else
{
q.rear->sled = novi;
q.rear = novi;
}
}
friend T DELETE_Q(Queue &q)
{
if (EMPTY_Q(q) == true) throw 12;
else
{
Elem *stari = q.front;
q.front = q.front->sled;
if (q.front == nullptr) q.rear = nullptr;
T br = stari->info;
delete stari;
return br;
}
}
~Queue()
{
while (front != nullptr) {
Elem *stari = front;
front = front->sled;
delete stari;
}
front = rear = nullptr;
}
};