-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.h
More file actions
362 lines (305 loc) · 8.43 KB
/
Copy pathdeque.h
File metadata and controls
362 lines (305 loc) · 8.43 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***************************************************************
* File: Deque.h
* Author: Ryan Walker
* Purpose: Contains the definition of the Deque class.
***************************************************************/
#ifndef Deque_H
#define Deque_H
#include <cassert>
#include <iostream>
using namespace std;
/************************************************
* Deque
* A double-ended queue that holds any data type
* and doubles in size when it's capacity is reached.
***********************************************/
template <class T>
class Deque
{
public:
T * data; // dynamically allocated array of T
int numItems; // how many items are currently in the Deque?
int cap; // how many items can I put on the Deque before full?
int myFront; // front item on the Deque
int myBack; // back item on the Deque
// Default constructor : empty and kinda useless
Deque() : myFront(0), myBack(0), numItems(0), cap(0), data(NULL) {}
// Copy constructor : copy it
Deque(const Deque & rhs) throw (const char *);
// Non-default constructor : pre-allocate
Deque(int cap) throw (const char *);
// Destructor : free everything
~Deque() { if (cap) delete [] data; }
// Assignment operator
Deque <T> & operator=(Deque <T> & rhs);
// Is the Deque empty?
bool empty() const { return numItems == 0; }
// Number of items within Deque
int size() const { return numItems; }
// Space available within Deque
int capacity() const { return cap; }
// Clears the Deque of items, not capacity
void clear() { numItems = 0; myFront = 0; myBack = 0; }
// Reallocates more space
void realloc();
// Adds an item to the front of the Deque
void push_front(const T & add) throw (const char *);
// Adds an item to the back of the Deque
void push_back(const T & add) throw (const char *);
// Removes the front item from the Deque
void pop_front() throw (const char *);
// Removes the back item from the Deque
void pop_back() throw (const char *);
// Returns the item at the front of the Deque
T & front() throw (const char *);
// Returns the item at the back of the Deques
T & back() throw (const char *);
};
/*******************************************
* Deque :: COPY CONSTRUCTOR
*******************************************/
template <class T>
Deque <T> :: Deque(const Deque <T> & rhs) throw (const char *)
{
assert(rhs.cap >= 0);
// do nothing if there is nothing to do
if (rhs.cap == 0)
{
cap = numItems = 0;
data = NULL;
return;
}
// attempt to allocate
try
{
data = new T[rhs.cap];
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate buffer";
}
// copy over the cap, size, front, and back
assert(rhs.numItems >= 0 && rhs.numItems <= rhs.cap);
cap = rhs.cap;
numItems = rhs.numItems;
myFront = rhs.myFront;
myBack = rhs.myBack;
// copy the items over one at a time using the assignment operator
for (int i = 0; i < cap; i++)
data[i] = rhs.data[i];
}
/**********************************************
* Deque : NON-DEFAULT CONSTRUCTOR
* Preallocate the Deque to "cap"
**********************************************/
template <class T>
Deque <T> :: Deque(int cap) throw (const char *)
{
assert(cap >= 0);
// do nothing if there is nothing to do
if (cap == 0)
{
this->cap = this->numItems = 0;
this->data = NULL;
return;
}
// attempt to allocate
try
{
data = new T[cap];
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate buffer";
}
// copy over the cap, size, front, and back
this->cap = cap;
this->numItems = 0;
myFront = 0;
myBack = 0;
// initialize the Deque by calling the default constructor
for (int i = 0; i < cap; i++)
data[i] = T();
}
/************************************
* Deque :: operator=
* Overrides = to copy any value.
************************************/
template <class T>
Deque <T> & Deque <T> :: operator=(Deque <T> & rhs)
{
// stop those memory leaks
delete [] data;
//copy over the cap, size, front, and back
cap = rhs.cap;
numItems = rhs.numItems;
data = new T[cap];
myFront = rhs.myFront;
myBack = rhs.myBack;
// copy the data
for (int i = 0; i < numItems; i++)
{
data[i] = rhs.data[i];
}
return *this;
}
/*******************************************
* Deque :: pop_front
* Removes the front item off the Deque.
*******************************************/
template <class T>
void Deque <T> :: pop_front() throw (const char *)
{
if (!size())
{
throw "ERROR: unable to pop from the front of empty deque";
}
// move the front
myFront = (myFront + 1) % cap;
numItems--;
}
/*******************************************
* Deque :: pop_back
* Removes the front item off the Deque.
*******************************************/
template <class T>
void Deque <T> :: pop_back() throw (const char *)
{
if (!size())
{
throw "ERROR: unable to pop from the back of empty deque";
}
// move the back
myBack--;
if (myBack < 0)
{
myBack = cap - 1;
}
numItems--;
}
/******************************************
* Deque :: front
* Returns the front item on the Deque
*******************************************/
template <class T>
T & Deque <T> :: front() throw (const char *)
{
// make sure the deque isn't empty or front isn't less than zero
if (numItems == 0)
throw "ERROR: unable to access data from an empty deque";
else
return data[myFront];
}
/*******************************************
* Deque :: back
* Returns the back item on the Deque
*******************************************/
template <class T>
T & Deque <T> :: back() throw (const char *)
{
// make sure the deque isn't empty or back isn't less than zero
if (numItems == 0)
throw "ERROR: unable to access data from an empty deque";
else if (myBack == 0)
return data[(myBack - 1 + cap) % cap];
else
return data[myBack - 1];
}
/*******************************************
* Deque :: realloc
* Called by the push_back and push_front
* functions. If the deque is full it will
* copy the data into a new deque twice the
* size.
*******************************************/
template <class T>
void Deque <T> :: realloc()
{
int oldCap = cap;
cap *= 2;
T *nData = new T[cap];
for (int i = 0; i < numItems; i++)
{
// copy the data
nData[i] = data[((myFront + i) % oldCap)]; // circular Deque. Copy them in order
}
delete [] data;
data = nData;
// reset front and back end
myFront = 0;
if (size())
myBack = numItems;
}
/*****************************************
* Deque :: push_front
* Adds an object onto front of the Deque
*****************************************/
template <class T>
void Deque <T> :: push_front(const T & add) throw (const char *)
{
try
{
// if the deque is full, reallocate to make space
if (cap == 0)
{
cap = 2;
data = new T[cap];
realloc();
}
else if (numItems >= cap)
{
realloc();
}
else if (((myBack % cap) == myFront) && (numItems == cap))
{
realloc();
}
// move the front
myFront--;
if (myFront < 0)
myFront = cap - 1;
// now we can add the new item
data[myFront] = add;
numItems++;
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate a new buffer for Deque";
}
}
/*****************************************
* Deque :: push_back
* Adds an object onto the back of the Deque
*****************************************/
template <class T>
void Deque <T> :: push_back(const T & add) throw (const char *)
{
try
{
// if the deque is full, reallocate to make space
if (cap == 0)
{
cap = 2;
data = new T[cap];
realloc();
}
else if (numItems >= cap)
{
realloc();
}
else if (((myBack % cap) == myFront) && (numItems == cap))
{
realloc();
cerr << "third realloc" << endl;
}
// now we can add the new item
data[myBack] = add;
myBack = (myBack + 1) % cap; // move the back
numItems++;
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate a new buffer for Deque";
}
}
#endif // Deque_H