-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_queue_as_array.h
More file actions
348 lines (274 loc) · 6.34 KB
/
Copy pathDynamic_queue_as_array.h
File metadata and controls
348 lines (274 loc) · 6.34 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef DYNAMIC_QUEUE_AS_ARRAY_H
#define DYNAMIC_QUEUE_AS_ARRAY_H
#include "ece250.h"
#include "Exception.h"
template <typename Object>
class Dynamic_queue_as_array
{
private:
int initial_size;
int array_size;
Object *array;
// other integer member variables, as necessary
int ihead;
int itail;
int count; //number of element in the queue
public:
Dynamic_queue_as_array( int = 10 );
Dynamic_queue_as_array( const Dynamic_queue_as_array & );
~Dynamic_queue_as_array();
Dynamic_queue_as_array &operator = ( const Dynamic_queue_as_array & );
Object head() const;
int size() const;
bool empty() const;
int capacity() const;
void enqueue( const Object & );
Object dequeue();
void clear();
// Friends
template <typename T>
friend std::ostream &operator << ( std::ostream &, const Dynamic_queue_as_array<T> & );
};
template <typename Object>
Dynamic_queue_as_array<Object>::Dynamic_queue_as_array( int n ) {
if (n <= 0) {
array = new Object[1];
}
else{
array = new Object[n];
}
count = 0;
itail = 0;
ihead = 0;
initial_size = n; //of array
array_size = n; //of array
}
template <typename Object>
Dynamic_queue_as_array<Object>::Dynamic_queue_as_array( const Dynamic_queue_as_array<Object> &queue ) {
// Initialize your object and then call operator=
*this = queue;
}
template <typename Object>
Dynamic_queue_as_array<Object>::~Dynamic_queue_as_array() {
delete[] array;
}
template <typename Object>
Dynamic_queue_as_array<Object> &Dynamic_queue_as_array<Object>::operator = ( const Dynamic_queue_as_array<Object> &rhs ) {
// empty the current queue, if necessary, deallocating memory
// make a copy of the queue rhs
if(rhs.empty())
{
return *this;
}
if (capacity()!= rhs.capacity()) {
delete array;
Object *new_array;
new_array = new Object[rhs.capacity()];
array_size = rhs.capacity();
array = new_array;
}
//now both array sizes are the same
else {
while (count != 0) {
dequeue();
}
}
//at this point our queue is empty and array size is the same as rhs
if (rhs.size() == 0) {
//already have an exact copy
return *this;
}
else if(rhs.ihead > rhs.itail){
//copy such that exact copy is made, thus no normalization implemented
for (int i = 0; i<= itail; ++i) {
array[i] = rhs.array[i];
}
for (int i = ihead; i<array_size; ++i) {
array[i] = rhs.array[i];
}
}
else {
//copy like normal
for (int i = 0; i<array_size; ++i) {
array[i] = rhs.array[i];
}
}
}
template <typename Object>
int Dynamic_queue_as_array<Object>::size() const {
return count;
}
template <typename Object>
int Dynamic_queue_as_array<Object>::capacity() const {
return array_size;
}
template <typename Object>
bool Dynamic_queue_as_array<Object>::empty() const {
if (count == 0) {
return true;
}
else {
return false;
}
}
template <typename Object>
Object Dynamic_queue_as_array<Object>::head() const {
if (count == 0) {
throw underflow();
}
return array[ihead];
}
template <typename Object>
void Dynamic_queue_as_array<Object>::enqueue( const Object &obj ) {
int tmp_size;
//if array was full and thus size needed to be doubled
if(count == array_size)
{
tmp_size = array_size * 2;
Object *tmp = new Object[tmp_size];
//if wrap around present
if(itail<ihead)
{
int q = 0;
//head to end
for (int i = ihead; i<array_size; ++i) {
tmp[q] = array[i];
++q;
}
//front to tail
for (int i = 0; i<=itail; ++i) {
tmp[q] = array[i];
++q;
}
ihead = 0;
itail = array_size - 1;
delete[] array; //delete smaller array because now we have the populated expanded one
array = tmp;
array[q] = obj;
//enqueue
++itail;
array[itail] = obj;
}
//no wrap around
else {
for (int i = 0; i<array_size; ++i) {
tmp[i] = array[i];
}
ihead = 0;
//set to last element in queue
itail = array_size -1;
delete[] array; //delete smaller array because now we have the populated expanded one
array = tmp;
//actual enqueue action
++itail;
array[itail] = obj;
}
//update to doubles size
array_size = tmp_size;
}
//if array not logically full but reached physical end
else
{
if(itail==(array_size-1))
{
//wrap around and enqueue
itail = 0;
array[itail] = obj;
}
else if (count == 0) {
array[itail] = obj;
}
else {
//normal enqueue
++itail;
array[itail] = obj;
}
}
++count;
}
template <typename Object>
Object Dynamic_queue_as_array<Object>::dequeue() {
if (count == 0)
{
throw underflow();
}
Object hold;
hold = array[ihead];
if (ihead==array_size-1)
{
if (itail<ihead)
{
//there is a wrap
ihead = 0;
}
//must be ihead = itail case so one element
else {
//do nothing except reset head and tail pointers to initial condition
ihead = 0;
itail = 0;
}
}
//one element only in queue
else if(count == 1){
//do nothing except reset pointers to initial condition
ihead = 0;
itail = 0;
}
else
{
//normal
++ihead;
}
--count;
if (count == (1/4) * array_size)
{
if ((1/2) * array_size >= initial_size) {
int tmp_size = (1/2) * array_size;
Object *tmp = new Object[tmp_size];
//wrapped
if (itail<ihead) {
int q = 0;
for (int i = ihead; i < array_size; ++i) {
tmp[q] = array[i];
++q;
}
for (int i = 0; i<= itail; ++i) {
tmp[q] = array[i];
++q;
}
}
//if not wrapped
else {
for (int i = 0; i<array_size; ++i) {
tmp[i] = array[i];
}
}
ihead = 0;
itail = array_size - 1; //reset to reflect normalized array
array = tmp;
array_size = tmp_size;
}
}
//return the thing we are dequeueing
return hold;
}
template <typename Object>
void Dynamic_queue_as_array<Object>::clear() {
itail = 0;
ihead = 0;
count = 0;
array_size = initial_size;
}
// You can modify this function however you want: it will not be tested
template <typename T>
std::ostream &operator << ( std::ostream &out, const Dynamic_queue_as_array<T> &queue ) {
// I don't know how you are implementing your queue so I cannot print it.
// If you want, you can print whatever you want here and then call cout
// in the driver.
// Remember to redirect to out, e.g.,
// out << "Hello!";
return out;
}
// Is an error showing up in ece250.h or elsewhere?
// Did you forget a closing '}' ?
#endif