-
Notifications
You must be signed in to change notification settings - Fork 0
/
llp.cpp
359 lines (339 loc) · 7.71 KB
/
llp.cpp
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
#include <stdlib.h>
#include <cassert>
#include <iostream>
using namespace std;
#include "Document.h"
#include "llp.h"
/*
* default constructor that creates a list of no length
* pointing the head, initializing the head and null pointers
*
* initializer list
*/
Double_list::Double_list (): size(0), head(NULL), tail(NULL)
{}
Double_list::Double_list (const Double_list& a_list)
{
if (a_list.head == NULL) {
head = NULL;
tail = NULL;
/*
* checks to see if list is empty, if so, set the head to null
* and you're finished, otherwise go through the deep copy set
*/
} else {
size = 0;
assert (size == 0);
Double_node *target = a_list.head;
while (target != NULL) {
doc_add (target->doc);
target = target->next;
size++;
}
}
}
/*
* this will remove the first element of the loop on EACH
* iteration of the loop.
*
*/
Double_list::~Double_list()
{
while (!is_empty()) {
kill();
}
}
/*
* Checks if the list's size = 0 and makes sure that head
* and tail are pointing to null. An assert statement
* may not be the best way to check that head & tail are set to NULL
*/
bool Double_list::is_empty () const
{
if (size == 0) {
return true;
} else {
return false;
}
}
/* Accesses the private size variable */
int Double_list::get_length () const
{
return size;
}
/*
* doc_add - this should be used for UNORDERED lists,
* such as queues, where adding to the tail, without indexing
* won't cause problems.
*/
void Double_list::doc_add (Document *new_doc)
{
/* Two cases (1) the list is empty, and (2) the list isn't empty
* CASE 1 - add initial node to the list, populate the doc
*/
Double_node *data_ptr = new Double_node;
data_ptr->doc = new_doc;
if (size == 0) {
head = data_ptr;
tail = data_ptr;
data_ptr->prev = NULL;
data_ptr->next = NULL;
/*
* CASE 2 - add on to the end. The head pointer doesn't
* change because of the tail-addition.
*/
} else if (size > 0) {
data_ptr->prev = tail;
tail->next = data_ptr;
data_ptr->next = NULL;
tail = data_ptr;
}
size++;
}
/*
* Not in use - doc add being used
*/
void Double_list::enqueue(Document* new_doc)
{
Double_node *data_ptr = new Double_node;
data_ptr->doc = new_doc;
if (size == 0) {
head = data_ptr;
tail = data_ptr;
data_ptr->prev = NULL;
data_ptr->next = NULL;
/*
* CASE 2 - add on to the end. The head pointer doesn't
* change because of the tail-addition. -- Also,
* this will affect formatting.
*/
} else if (size > 0) {
data_ptr->prev = tail;
data_ptr->next = NULL;
tail->next = data_ptr;
tail = data_ptr;
// data_ptr->next = head;
// head->prev = data_ptr;
// data_ptr->prev = NULL;
// head = data_ptr;
}
size++;
}
bool Double_list::remove (string name)
{
/* WORKS!!!!!!
* you should throw an exception
* find */
Double_node *target = find (name);
if (target == NULL) {
return false;
/* CASE 1 - this is the first node, pointed at by head
*/
} else if (size == 1) {
target->prev = NULL;
target->next = NULL;
head = NULL;
tail = NULL;
delete target;
size = 0;
return true;
/* CASE 2 - doc is located at the tail, the edge case
* of the size = 1, where head = node = tail, is
* already handled.
*/
} else if (tail == target) {
tail = target->prev;
target->prev->next = NULL;
delete target;
target = NULL;
size--;
return true;
/* CASE 3 - if target is first doc in list with more than
* one doc.
*/
} else if ((size > 1) && (head == target)) {
head = target->next;
target->next->prev = NULL;
delete target;
target = NULL;
size--;
return true;
/* CASE 4 - the doc is somewhere in the middle of the list
* this only requires, finding it, moving several
* pointers, deleting, decrementing size.
*/
} else if ((size > 1) &&
(head != target) &&
(tail != target)) {
target->prev->next = target->next;
target->next->prev = target->prev;
target = NULL;
delete target;
size--;
return true;
}
}
/*
* pop routine that doesn't throw a pointer, just deletes
* the docs
*/
void Double_list::kill()
{
if (is_empty()) {
size = 0;
} else {
Double_node *target = tail;
if (size > 1) {
tail = tail->prev;
size--;
delete target;
target = NULL;
} else if (size == 1) {
delete target;
tail = NULL;
head = NULL;
target = NULL;
size = 0;
}
}
}
Document* Double_list::pop()
{
/* WORKS!
* 3 cases -
* (1) empty list
* (2) one element in list
* (3) more than one element in list
*/
if (is_empty()) {
size = 0;
return NULL;
} else {
Double_node *target = tail;
Document* doc;
if (size > 1) {
doc = target->doc;
tail = tail->prev;
delete target;
target = NULL;
size--;
return doc;
} else if (size == 1) {
doc = head->doc;
delete target;
tail = NULL;
head = NULL;
target = NULL;
size = 0;
return doc;
}
}
}
/*
* pop from head, return pointer to doc.
* the problem is, front is pointing to the TAIL
* in main.cpp, not the technical head. So you need to switch your
* en/dequeue functions around.
*/
Document* Double_list::dequeue()
{
if (is_empty()) {
size = 0;
return NULL;
} else {
/* the list is input backwards. change how items are added so it looks
* pretty enough for the test to pass.
*/
Double_node *target = head;
Document* doc;
if (size > 1) {
doc = target->doc;
head = head->next;
delete target;
target = NULL;
size--;
return doc;
} else if (size == 1) {
doc = target->doc;
delete target;
target = NULL;
tail = NULL;
head = NULL;
size = 0;
return doc;
}
}
}
/*
* taken from stock code
*/
void Double_list::print(ostream& os)
{
Double_node* cur = head;
while (cur != NULL) {
os << cur->doc->get_name() << " ";
cur = cur->next;
}
}
/* modified for queue, i = 0 is from head to tail, i = 1 tail to head
*/
void Double_list::print_back(ostream& os)
{
Double_node* cur = tail;
if (size > 1) {
while (cur != NULL) {
os << cur->doc->get_name() << " ";
cur = cur->prev;
}
/* I wrote a bug and am having trouble finding it. This is hopefully a
* temporary fix.
*/
} else {
os << cur->doc->get_name() << " ";
}
}
/*
* PRIVATE find method that should search the
* list for the data doc once found, it should
* return a pointer to the node. Now other functions
* can use find,
*/
Double_node* Double_list::find (string name) const
{
/* WORKS!!!!!!
*/
Double_node *target = head;
int inc = 1;
/* loop through the docs. If the counter is ever greater than size, then
* the doc isn't in the list, return a NULL pointer.
*
* You should throw an expception instead of a pointer.
*/
while (inc <= (size + 1)) {
if (inc == (size + 1)) {
return NULL;
} else if (target->doc->get_name() == name) {
return target;
} else {
target = target->next;
inc++;
}
}
}
/* WORKS!!!
*
* throw blocks seem useless if they completely halt the program
* though, if the object isn't found, and you try to access any of its members
* the program barfs and dies.
*/
Document* Double_list::retrieve(string name) const //throw(No_such_object_exception)
{
Double_node *found = find(name);
if (found == NULL) {
return NULL;
// throw No_such_object_exception
("No_such_object_exception: the object you seek doesn't exist");
} else {
return found->doc;
}
}