forked from tmont004/Virtual-Library-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
230 lines (181 loc) · 6.39 KB
/
Copy pathqueue.cpp
File metadata and controls
230 lines (181 loc) · 6.39 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef QUEUE_H
#define QUEUE_H
#include <cassert>
#include <iostream>
//First in First out Queue classes
//Based on textbook ch 17
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class queueADT
{
public:
virtual bool isEmptyQueue() const = 0;
//Function to return wheter the queue is empty
virtual bool isFullQueue() const = 0;
//Function to return whether the queue is full
virtual void initializeQueue() const = 0;
//Function to initialize the queue
//Postcondition: the queue is empty
virtual Type front() const = 0;
//Function to return the first element in the queue
//Precondition: the queue exists and is not empty
virtual Type back() const = 0;
//Function to return the last element in the queue
//Precondition: the queue exists and is not empty
virtual void addQueue(const Type& queueElement) = 0;
//Function to add queueElement to the queue
//Precondition: The queue exists and in not full
virtual void deleteQueue() = 0;
//Function to remove the first element of the queue
//Precondition: The queue exists and is not empty
};
template <class Type>
class linkedQueueType: public queueADT<Type>
{
public:
const linkedQueueType<Type>& operator=(const linkedQueueType<Type>&);
//Overload the assignment operator
bool isEmptyQueue() const;
//Function to return whether the queue is empty
bool isFullQueue() const;
//Function to return whether the queue is full
void initializeQueue();
//Function to initialize the queue
//Postcondition: The queue is empty
Type front() const;
//Function to return the first element in the queue
//Precondition: The queue exists and is not empty
Type back() const;
//Function to return the last element in the queue
//Precondition: The queue exists and is not empty
void addQueue(const Type& queueElement);
//Function to add queueElement to the queue
//Precondition: The queue exists and is not full
void deleteQueue();
//Function to remove the first element in the queue
//Precondition The queue exists and is not empty
linkedQueueType();
//Default constructor
linkedQueueType(const linkedQueueType<Type>& otherQueue);
//Copy constructor
~linkedQueueType();
//Destructor
protected:
nodeType<Type> *queueFront; //Pointer to the front of the queue
nodeType<Type> *queueRear; //Pointer to the rear of the queue
};
template <class Type>
bool linkedQueueType<Type>::isEmptyQueue() const
{
return (queueFront == nullptr);
}//end isEmptyQueue
template <class Type>
bool linkedQueueType<Type>::isFullQueue() const
{
return false;
}//end isFullQueue
template <class Type>
void linkedQueueType<Type>::initializeQueue()
{
nodeType<Type> *temp;
while (queueFront != nullptr) //While there are elements left in the queue
{
temp = queueFront; //Set temp to point to the current node
queueFront = queueFront->link; //Advance first to the next node
delete temp; //deallocate memory pointed to by temp
}
queueRear = nullptr; //Set rear to nullptr
}//end initializeQueue
template <class Type>
void linkedQueueType<Type>::addQueue(const Type& newElement)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>; //Create the node
newNode->info = newElement; //Store the info
newNode->link = nullptr; //Initialize the link field to nullptr
if (queueFront == nullptr) //If initially the queue is empty
{
queueFront = newNode; //Set the newNode as the first
queueRear = newNode; //and last node
}
else //Otherwise, add newNode at the end
{
queueRear->link = newNode;
queueRear = queueRear->link;
}
}//end addQueue
template <class Type>
Type linkedQueueType<Type>::front() const
{
assert (queueFront != nullptr);
return queueFront->info;
}//end front
template <class Type>
Type linkedQueueType<Type>::back() const
{
assert (queueRear != nullptr);
return queueRear->info;
}//end back
template <class Type>
void linkedQueueType<Type>::deleteQueue()
{
nodeType<Type> *temp;
if (!isEmptyQueue())
{
temp = queueFront; //Set temp to point to the first elment in the queue
queueFront = queueFront->link; //Advance queueFront
delete temp; //Delete the first node
if (queueFront == nullptr) //If after deletion the queue is empty
{
queueRear = nullptr; //Set queueRear to nullptr
}
}
else
std::cout << "Cannot remove from an empty queue" << std::endl;
}//end deleteQueue
template <class Type>
linkedQueueType<Type>::linkedQueueType()
{
queueFront = nullptr; //Set front to nullptr
queueRear = nullptr; //Set rear to nullptr
}//end default constructor
template <class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
nodeType<Type> *newNode, *current, *last;
if (queueFront != nullptr) //If queue is not empty, make it empty
initializeQueue();
if (otherQueue.front == nullptr)
queueFront = nullptr;
else
{
current = otherQueue.front; //Set current to point to the front of the queue to be copied
//Copy the front element in the queue
queueFront = new nodeType<Type>;//Create the node
queueFront->info = current->info;//Copy the info
queueFront->link = nullptr; //Set the link field of the node to nullptr
last = queueFront; //Set last to point to the node
current = current->link; //Set current to point to the next node
//Copy the remaining elements in the queue
while (current != nullptr)
{
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current = current->link;
}//end while
}//end else
}//end copy constructor
template <class Type>
linkedQueueType<Type>::~linkedQueueType()
{
initializeQueue();
}//end destructor
#endif //QUEUE_H