-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.h
176 lines (146 loc) · 3.04 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**~*~*
Queue template
*~**/
//
// Queue.h
//
// This is the header file for the Queue class.
//
// Minh An Cao
//
#ifndef DYNAMICQUEUE_H
#define DYNAMICQUEUE_H
#include <iostream>
using namespace std;
template <class T>
class Queue
{
private:
// Structure for the queue nodes
struct QueueNode
{
T value; // Value in the node
QueueNode *next; // Pointer to next node
};
QueueNode *front; // Pointer to the queue front
QueueNode *rear; // Pointer to the queue rear
int count;
public:
//Constructor
Queue(){front = rear = NULL; count = 0;}
// Destructor
~Queue();
// Stack operations
bool enqueue(T);
bool dequeue(T &);
bool isEmpty();
int getCount();
bool queueFront(T &);
bool queueRear(T &);
};
/**~*~*
Destructor
*~**/
template <class T>
Queue<T>::~Queue()
{
QueueNode *currNode, *nextNode;
// Position nodePtr at the top of the stack.
currNode = front;
// Traverse the list deleting each node.
while (currNode) //while (currNode != NULL)
{
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}
/**~*~*
Member function getCount returns
the number of elements in the queue
*~**/
template <class T>
int Queue<T>::getCount()
{
return count;
}
/**~*~*
Member function isEmpty returns true if the stack
is empty, or false otherwise.
*~**/
template <class T>
bool Queue<T>::isEmpty()
{
return count == 0;
}
/**~*~*
Member function enqueue inserts the argument into
the queue.
*~**/
template <class T>
bool Queue<T>::enqueue(T item)
{
QueueNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new QueueNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = NULL;
if( front == NULL ) // insert to an empty queue
front = newNode;
else
rear->next = newNode;
count++;
rear = newNode;
return true;
}
/**~*~*
Member function dequeue deletes the value at the front
of the queue, and copies it into the variable
passed as an argument.
*~**/
template <class T>
bool Queue<T>::dequeue(T &item)
{
QueueNode *pDel; // Temporary pointer
// empty queue
if (count == 0)
return false;
// delete the value at the front of the queue
item = front->value;
pDel = front;
if( count == 1 )
rear = front = NULL;
else
front = front->next;
count--;
delete pDel;
return true;
}
/**~*~*
Member function queueFront copies the value at the front
of the queue into the variable passed as an argument.
*~**/
template <class T>
bool Queue<T>::queueFront(T &item)
{
if( front == NULL )
return false;
item = front->value;
return true;
}
/**~*~*
Member function queueRear copies the value at the rear
of the queue into the variable passed as an argument.
*~**/
template <class T>
bool Queue<T>::queueRear(T &item)
{
if( rear == NULL )
return false;
item = rear->value;
return false;
}
#endif