-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeList.cpp
334 lines (186 loc) · 6.43 KB
/
EdgeList.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
/************************************************************************
* *
* *
* Name:Patrick Leonard *
* Assignment: Program 3 *
* Class: CS 202 *
* Date: 11/15/12 *
* File: EdgeList.cpp *
* *
* This .cpp file implements the EdgeList class. This is the list *
*of Edge objects that represents the connections between Class objects *
*in the graph abstract data type. It is a linear linked list with a *
*pointer to the first Edge object in the list as a member variable. *
uAll traversals in the list are recursive. This is also the base class *
*for the Class class. The pointer to the first Edge is protected in *
*terms of visibility so the Class class has direct access to the first *
*Edge object. *
* *
************************************************************************/
#include "EdgeList.h"
//Default constructor
EdgeList::EdgeList(): first(NULL)
{
}
//Copy consructor, accepts an EdgeList object by referece
EdgeList::EdgeList(const EdgeList &source): first(NULL)
{
Edge* copyLink = source.first;
copyHelper(copyLink); //Call to recursive copy helper with first link in input EdgeList
}
//Overloaded assignment operator, uses EdgeList object as right operand
EdgeList& EdgeList::operator=(const EdgeList &source)
{
//Check for self assignment
if(this == &source)
{
return *this;
}
//Check is deallocation of member is needed
if(first != NULL)
{
edgeDestructHelper(first); //call recursive destructor helper
}
//Perform same operations as copy constructor
Edge* copyLink = source.first;
copyHelper(copyLink);
return *this; //return a reference to this object
}
//Helper function to copy the link list recursively, accepts pointer to first Edge in list
void EdgeList::copyHelper(Edge* copyLink)
{
if(copyLink == NULL) //If passed end of list stop
{
return;
}
Edge* copyEdge = new Edge(*copyLink); //Create a new Edge using copy constructor
this->copyInsert(copyEdge); //Use copyInsert to add to the lsit
copyHelper(copyLink->getNext()); //Recursive call to next link
}
//Helper insert function for copy constructor, accepts pointer to Edge object
void EdgeList::copyInsert(Edge* copyEdge)
{
Edge* temp = NULL; //temp pointer to Edge
if(first == NULL) //If list is empty pointer first to input pointer
{
first = copyEdge;
}
else
{
temp = traverseEnd(first); //Go to the end of the list
temp->setNext(copyEdge); //Point the end's next to the input pointer
}
}
//Class destructor
EdgeList::~EdgeList()
{
edgeDestructHelper(first); //Call to recursive helper function with start of list
}
//Recusive helper function for the destructor, accepts pointer to the start of the EdgeList
void EdgeList::edgeDestructHelper(Edge* destroy)
{
Edge* temp = NULL; //temp pointer to Edge
if(destroy == NULL) //If passed the end of list stop
{
return;
}
temp = destroy; //Assign input pointer to temp
edgeDestructHelper(destroy->getNext()); //recusive call to next Edge
delete temp; //delete the Edge object
}
//Adds and Edge to the list, accepts and integer and a character array as arguments
void EdgeList::addEdge(int index, const char descInput[])
{
Edge* newEdge = new Edge(index, descInput); //Create new Edge with inputs as arguments
Edge* temp = NULL; //temp pointer to Edge
if(first == NULL) //If list is empty point first to new Edge
{
first = newEdge;
}
else
{
temp = traverseEnd(first); //Go to end of list
temp->setNext(newEdge); //Set end's next to new Edge
}
}
//Recursive function to get to the end of the list, accepts pointer to the start of list
Edge* EdgeList::traverseEnd(Edge* start)
{
if(start->getNext() == NULL) //If at the end of the list return pointer
{
return start;
}
traverseEnd(start->getNext()); //Recursive call to next Edge
}
//Removes Edge from the list matching the integer argument
void EdgeList::removeEdge(int index)
{
Edge* temp = NULL; //temp pointer to Edge
Edge* delEdge = NULL; //pointer to Edge to be deleted
if(first == NULL) //If EdgeList is empty says to and return
{
std::cout << "\n\n---/---/--Edge list is empty!--/---/---\n" << std::endl;
return;
}
if(first->getConnection() == index) //If Edge to be deleted is first Edge
{
temp = first; //Assign first to temp
first = temp->getNext(); //Set new first
delete temp; //Delete the Edge object
return;
}
else
{
temp = removeHelper(index, first); //Find Edge before one to be deleted
if(temp == NULL) //If passed the end of list, not found and return
{
std::cout << "\n\n---/---/--Edge not found!--/---/---\n" << std::endl;
return;
}
delEdge = temp->getNext(); //Assign pointer to Edge to be deleted
temp->setNext(delEdge->getNext()); //Connect list properly
delete delEdge; //Delete Edge object
return;
}
}
//Recursive helper function to find the Edge before the one to be removed
//Accepts an integer and a pointer to an Edge as arguments
Edge* EdgeList::removeHelper(int index, Edge* start)
{
if(start == NULL) //If passed end of list return NULL
{
return NULL;
}
if(start->getNext()->getConnection() == index) //If index matches to next Edge
{
return start; //Return edge before one to be deleted
}
removeHelper(index, start->getNext()); //Recursive call to next Edge
}
//Displays the list by calling recurseDisplay()
void EdgeList::displayList()
{
if(first == NULL) //If list is empty says so
{
std::cout << "\n\n---/---/--Edge List is empty!--/---/---\n" << std::endl;
return;
}
recurseDisplay(first); //call recursiveDiaplsy() with first in list as argument
return;
}
//Recursive display function that properly displays the connection and description
//Accepts a pointer to an Edge object as argument
void EdgeList::recurseDisplay(Edge* start)
{
if(start == NULL) //if passed end of list then stop
{
return;
}
else
{
//Display Edge properly
std::cout << "\nEdge connection number: " << start->getConnection() << std::endl;
start->displayDescription();
recurseDisplay(start->getNext()); //recursive call to next Edge in list
}
}