-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueAsArray.h
216 lines (175 loc) · 5.89 KB
/
queueAsArray.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
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
//Header file QueueAsArray
#ifndef H_QueueAsArray
#define H_QueueAsArray
#include <iostream>
#include <cassert>
#include "queueADT.h"
using namespace std;
template <class Type>
class queueType : public queueADT<Type>
{
public:
const queueType<Type>& operator=(const queueType<Type>&);
//Overload the assignment operator.
bool isEmptyQueue() const;
//Function to determine whether the queue is empty.
//Postcondition: Returns true if the queue is empty,
// otherwise returns false.
bool isFullQueue() const;
//Function to determine whether the queue is full.
//Postcondition: Returns true if the queue is full,
// otherwise returns false.
void initializeQueue();
//Function to initialize the queue to an empty state.
//Postcondition: The queue is empty.
Type front() const;
//Function to return the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the first
// element of the queue is returned.
Type back() const;
//Function to return the last element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the last
// element of the queue is returned.
void addQueue(const Type& queueElement);
//Function to add queueElement to the queue.
//Precondition: The queue exists and is not full.
//Postcondition: The queue is changed and queueElement
// is added to the queue.
void deleteQueue();
//Function to remove the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: The queue is changed and the first
// element is removed from the queue.
queueType(int queueSize = 100);
//Constructor
queueType(const queueType<Type>& otherQueue);
//Copy constructor
~queueType();
//Destructor
private:
int maxQueueSize; //variable to store the maximum queue size
int count; //variable to store the number of
//elements in the queue
int queueFront; //variable to point to the first
//element of the queue
int queueRear; //variable to point to the last
//element of the queue
Type* list; //pointer to the array that holds
//the queue elements
void copyQueue(const queueType<Type>& otherQueue);
};
template <class Type>
bool queueType<Type>::isEmptyQueue() const
{
return (count == 0);
} //end isEmptyQueue
template <class Type>
bool queueType<Type>::isFullQueue() const
{
return (count == maxQueueSize);
} //end isFullQueue
template <class Type>
void queueType<Type>::initializeQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
} //end initializeQueue
template <class Type>
Type queueType<Type>::front() const
{
assert(!isEmptyQueue());
return list[queueFront];
} //end front
template <class Type>
Type queueType<Type>::back() const
{
assert(!isEmptyQueue());
return list[queueRear];
} //end back
template <class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
if (!isFullQueue())
{
queueRear = (queueRear + 1) % maxQueueSize; //use mod
//operator to advance queueRear
//because the array is circular
count++;
list[queueRear] = newElement;
}
else
cout << "Cannot add to a full queue." << endl;
} //end addQueue
template <class Type>
void queueType<Type>::deleteQueue()
{
if (!isEmptyQueue())
{
count--;
queueFront = (queueFront + 1) % maxQueueSize; //use the
//mod operator to advance queueFront
//because the array is circular
}
else
cout << "Cannot remove from an empty queue." << endl;
} //end deleteQueue
//Constructor
template <class Type>
queueType<Type>::queueType(int queueSize)
{
if (queueSize <= 0)
{
cout << "Size of the array to hold the queue must "
<< "be positive." << endl;
cout << "Creating an array of size 100." << endl;
maxQueueSize = 100;
}
else
maxQueueSize = queueSize; //set maxQueueSize to
//queueSize
queueFront = 0; //initialize queueFront
queueRear = maxQueueSize - 1; //initialize queueRear
count = 0;
list = new Type[maxQueueSize]; //create the array to
//hold the queue elements
} //end constructor
//Destructor
template <class Type>
queueType<Type>::~queueType()
{
delete[] list;
} //end destructor
template <class Type>
void queueType<Type>::copyQueue
(const queueType<Type>& otherQueue)
{
delete[] list;
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
list = new Type[maxQueueSize];
//copy otherStack into this stack
for (int j = 0; j < count; j++)
list[j] = otherQueue.list[j];
} //end copyStack
template <class Type>
const queueType<Type>& queueType<Type>::operator=
(const queueType<Type>& otherQueue)
{
if (this != &otherQueue) //avoid self-copy
queueType(otherQueue);
return *this;
cout << "Write the definition of the function "
<< "to overload the assignment operator." << endl;
} //end assignment operator
template <class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
list = nullptr;
copyQueue(otherQueue);
} //end copy constructor
#endif