-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_queue.h
105 lines (89 loc) · 1.71 KB
/
template_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
103
104
105
/*
* template_queue.h
*
* Created on: Dec 13, 2016
* Author: alex
*/
#ifndef TEMPLATE_QUEUE_H_
#define TEMPLATE_QUEUE_H_
#include <iostream>
template<class T>
class Queue {
struct QueueNode {
T data;
QueueNode *next;
QueueNode(const T& newData, QueueNode *nextNode) :
data(newData), next(nextNode) {
}
QueueNode(QueueNode*nextNode) :
next(nextNode) {
}
};
QueueNode *front, *rear;
int size;
public:
Queue() {
front = NULL;
rear = NULL;
size = 0;
//std::cout << "Queue was constructed" << std::endl;
}
~Queue() {
//std::cout << "Queue was destructed" << std::endl;
while (!isEmpty()) {
Dequeue();
}
isEmpty();
}
void Enqueue(const T& object);
T Dequeue();
const T& GetfrontData();
int GetSize();
bool isEmpty();
void Print();
};
template<class T>
void Queue<T>::Enqueue(const T& obj) {
if (size == 0) {
front = new QueueNode(obj, rear);
rear = front;
} else {
//std::cout << "queue insert" << std::endl;
QueueNode* tmp = new QueueNode(obj, NULL);
rear->next = tmp;
rear = tmp;
}
size++;
//std::cout << "exiting" << std::endl;
}
template<class T>
T Queue<T>::Dequeue() {
if (this->isEmpty() == 0) {
QueueNode *frontNode = front;
T tdata = frontNode->data;
front = front->next;
delete frontNode;
frontNode = NULL;
size--;
//std::cout << "Dequeued succefully! #" << size << std::endl;
return tdata;
} else {
return static_cast<T>(NULL);
}
}
template<class T>
const T& Queue<T>::GetfrontData() {
if (this->isEmpty() == 0) {
return front->data;
}
return static_cast<T>(NULL);
}
template<class T>
bool Queue<T>::isEmpty() {
return (size == 0);
}
template<class T>
int Queue<T>::GetSize() {
return size;
}
#endif /* TEMPLATE_QUEUE_H_ */