-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathqueue_array.h
More file actions
264 lines (221 loc) · 5.72 KB
/
queue_array.h
File metadata and controls
264 lines (221 loc) · 5.72 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
#pragma once
#include <iostream>
using namespace std;
const int DEFAULT_CAPACITY = 8;
// A Queue class based on a circular array implementation
template <class T>
class QueueArray
{
public:
QueueArray();
QueueArray(const QueueArray& other);
~QueueArray();
void enqueue(const T& val);
T dequeue();
void clear();
int get_size() const;
T get_first() const;
T get_last() const;
bool is_empty() const;
QueueArray& operator=(QueueArray other);
private:
T* data;
int capacity; // maximum number of elements allowed in the queue.
int size; // current number of elements in the queue.
int first; // index of the first element in the queue.
int last; // index of the last element in the queue.
bool is_full() const;
void resize(int new_cap);
};
// Constructor.
// Initially no values are in the queue,
// so first and last are -1.
template <class T>
QueueArray<T>::QueueArray()
{
capacity = DEFAULT_CAPACITY;
data = new T[capacity];
last = -1;
first = -1;
size = 0;
}
// Destructor.
template <class T>
QueueArray<T>::~QueueArray()
{
delete [] data;
}
template <class T>
T QueueArray<T>::get_first() const
{
if (is_empty())
throw string("ERROR: An empty queue has no first element");
return data[first];
}
template <class T>
T QueueArray<T>::get_last() const
{
if (is_empty())
throw string("ERROR: An empty queue has no last element");
return data[last];
}
template <class T>
bool QueueArray<T>::is_empty() const
{
return size == 0;
}
template <class T>
bool QueueArray<T>::is_full() const
{
return size == capacity;
}
template <class T>
int QueueArray<T>::get_size() const
{
return size;
}
template <class T>
void QueueArray<T>::clear()
{
first = -1;
last = -1;
size = 0;
delete [] data;
data = new T[DEFAULT_CAPACITY];
capacity = DEFAULT_CAPACITY;
}
// adds an element to the end of the queue (data[++last] = val).
//
// If the queue is empty, both first and last should be set to 0.
//
// If the queue is not full and the last element in the queue is at the last
// index in the array, then we wrap around and insert the new element at
// index 0.
//
// -----------------------------
// [ | | A | B | C | D | ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// first last
//
// After enqueue(E)
// -----------------------------
// [ | | A | B | C | D | E ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// first last
//
// After enqueue(F)
// -----------------------------
// [ F | | A | B | C | D | E ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// last first
//
template <class T>
void QueueArray<T>::enqueue(const T& val)
{
if (is_full())
resize(capacity * 2);
last = (last + 1) % capacity;
data[last] = val;
size++;
if (first == -1)
first = 0;
}
// Returns and deletes an element from the queue (val = data[first++]).
//
// If there is only one element, both first and last should be set to -1.
//
// If the queue is not empty and the first element in the queue is at the last
// index in the array, then we wrap around and first becomes 0.
//
// -----------------------------
// [ A | B | C | D | | E | F ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// last first
//
// After dequeue()
// -----------------------------
// [ A | B | C | D | | | F ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// last first
//
// After dequeue()
// -----------------------------
// [ A | B | C | D | | | ]
// -----------------------------
// 0 1 2 3 4 6 7
// ^ ^
// first last
template <class T>
T QueueArray<T>::dequeue()
{
if (is_empty())
throw string("ERROR: Can't remove from an empty queue.");
T val = data[first];
size--;
if (size == 0) {
last = -1;
first = -1;
}
else
first = (first + 1) % capacity;
if (size < capacity / 4 && capacity > DEFAULT_CAPACITY)
resize(capacity / 2);
return val;
}
// Copy constructor
template <class T>
QueueArray<T>::QueueArray(const QueueArray& other)
{
capacity = other.capacity;
size = other.size;
first = other.first;
last = other.last;
data = new T[capacity];
int j = other.first;
for (int i = 0; i < size; i++) {
data[j] = other.data[j];
j = (j + 1) % other.capacity;
}
}
template <class T>
QueueArray<T>& QueueArray<T>::operator=(QueueArray<T> other)
{
swap(data, other.data);
swap(first, other.first);
swap(last, other.last);
swap(capacity, other.capacity);
swap(size, other.size);
return *this;
}
template <class T>
void QueueArray<T>::resize(int new_cap)
{
if (new_cap < size)
throw string("ERROR: Invalid new capacity");
T* new_data = new T[new_cap];
int j = first;
for (int i = 0; i < size; i++) {
new_data[i] = data[j];
j = (j + 1) % capacity;
}
delete [] data;
data = new_data;
capacity = new_cap;
if (size == 0) {
first = -1;
last = -1;
} else {
first = 0;
last = size - 1;
}
}