-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlab_queue.cpp
More file actions
245 lines (176 loc) · 4.84 KB
/
Copy pathlab_queue.cpp
File metadata and controls
245 lines (176 loc) · 4.84 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
#include <exception>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Queue
{
private:
const int maxSize;
vector<char> queue;
public:
class Full: public exception
{
public:
virtual const char* what() const throw()
{
return "Queue is full";
}
};
class Empty: public exception
{
public:
virtual const char* what() const throw()
{
return "Queue is empty";
}
};
/** Initialise the queue, maxSize is the maximum number of
values that can be stored in the queue at any time **/
Queue( const int _maxSize ) : maxSize(_maxSize)
{
}
~Queue()
{
}
/** Returns the number of values currently stored in the
queue **/
int num_items() const
{
// COMPLETE ME
return 0;
}
/** Add value to the front of the queue, raises Queue::Full
exception is queue is full **/
void push( char value )
{
if( num_items() < maxSize )
{
queue.emplace_back(value);
}
else
{
throw Full();
}
}
/** Returns the value currently stored at the front of the
queue, raises Queue::Empty exception is queue is
empty **/
char front() const
{
if( num_items() > 0 )
{
return queue.front();
}
else
{
throw Empty();
}
}
/** Returns the value currently stored at the back of the
queue, raises Queue.Empty exception if the queue is
empty **/
char back() const
{
// COMPLETE ME
return 0;
}
/** Removes and returns the value currently stored at the
front of the queue, raises Queue::Empty exception is queue
is empty **/
char pop()
{
// COMPLETE ME
return 0;
}
};
/* ========================================================
Below this is the testing code for the queue class, feel
free to have a look but you don't need to worry about it
========================================================
*/
int main()
{
int errors = 0;
string testvalues = "abcde";
Queue q( testvalues.length() );
// === pushing test ======
for( int i=0; i<testvalues.length(); ++i )
{
char c = testvalues[i];
q.push( testvalues[i] );
cout << "Pushing " << c << endl;
cout << "Test front of queue" << endl;
try
{
if( q.front() != testvalues[0] )
{
cerr << "Error in front() - the front of the queue is wrong, expected " << testvalues[0] << " but got " << q.front() << endl;
errors += 1;
}
cout << "Test back of queue" << endl;
if( q.back() != c )
{
cerr << "Error in back() - last thing pushed was " << c << " but back of queue contains " << q.back() << endl;
errors += 1;
}
}
catch( Queue::Empty &error )
{
cerr << "Error - raised an Empty exception when there should be values in the queue" << endl;
errors += 1;
}
cout << "Test size of queue" << endl;
if( q.num_items() != i+1 )
{
cerr << "Error in num_items() - pushed " << i+1 << " values but queue reports size of " << q.num_items() << endl;
errors += 1;
}
}
// === is full test ======
try
{
q.push( 'f' );
cerr << "Error in push() - tried to push to a full queue but no exception" << endl;
errors += 1;
}
catch( Queue::Full& error )
{}
// === pop test ======
for( int i=0; i<testvalues.length(); ++i )
{
char c = testvalues[i];
char val = q.pop();
cout << "Popping " << val << endl;
if( val != c )
{
cerr << "Error in pop() - wrong value was popped from the queue, expecting " << c << " but got " << val << endl;
errors += 1;
}
int expectedsize = testvalues.length()-i-1;
if( q.num_items() != expectedsize )
{
cerr << "Error in num_items() - queue should have " << expectedsize << " values but claims it has " << q.num_items() << " values" << endl;
errors += 1;
}
}
// === empty test ======
try
{
q.pop();
cerr << "Error in pop() - tried to pop from an empty queue but no exception" << endl;
errors += 1;
}
catch( Queue::Empty& error )
{}
try
{
q.front();
cerr << "Error in front() - tried to get front off an empty queue but no exception" << endl;
errors += 1;
}
catch( Queue::Empty& error )
{}
cout << "Finished testing" << endl;
return errors;
}