-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
385 lines (311 loc) · 7.68 KB
/
LinkedList.h
File metadata and controls
385 lines (311 loc) · 7.68 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef LinkedList_h
#define LinkedList_h
#include "LinkNode.h"
#include <iostream>
#include <string>
//=====================================================================================
// Linked List
//=====================================================================================
template <typename U>
class LinkedList;
template <typename T>
class LinkedList
{
private:
LinkNode<T> *head;
LinkNode<T> *tail;
int count;
int sort; //0:unsorted 1:ascending 2:descending
public:
LinkedList(int = 0);
virtual ~LinkedList();
int getSort() const;//getter for a sorting type of a list
int getCount() const;//getter for a count
void emptyList();//clear out all nodes in a list
void print(std::ostream &) const;// prints out list
void addData(T); // create new node and add it to a list
int addData(T, int);// create new node and add it to a list to a given location
bool deleteData(T);//deletes one node of delData value
int findData(T);//find a node that has given data
bool isEmpty();//check if its empty
void setTail(); //already efficiently incorporated in the add and delete functions so this function exists only for potential future use
T& getTail();
};
/*Constructor for the LinkedList. Sets count to 0. Creates the head and makes it point to nullptr.
Pre: sortType - integer which determines if the data will be sorted or unsorted
Post: modifies the head next
Return:
*/
template <typename T>
LinkedList<T>::LinkedList(int sortType)
: count(0) //initialize count to 0
{
head = new LinkNode<T>;
head->setNext(nullptr);
tail = nullptr;
if (sortType < 0 || sortType > 2) //if sortType is neither 0, 1, or 2, set it to 0: unsorted
sortType = 0;
sort = sortType;
}
/* Destructor deletes all the nodes and the head. Frees memory
Pre:
Post: deltes everything.
Return:
*/
template <typename T>
LinkedList<T>::~LinkedList()
{
LinkNode<T> *currPtr;
LinkNode<T> *nextPtr;
currPtr = head->getNext();
while (currPtr != nullptr)
{
nextPtr = currPtr->getNext();
delete currPtr;
currPtr = nextPtr;
}
delete head;
}
/* prints the output of the array.
Pre:
Post:
Return:
*/
template <typename T>
void LinkedList<T>::print(std::ostream &os) const
{
LinkNode<T> *currPtr;
currPtr = head->getNext();
while (currPtr != nullptr)
{
os << currPtr->getData() << std::endl;
currPtr = currPtr->getNext();
}
}
/* Adds data to the link list. Creates a new node.
Pre: newData - type T. template type
Post:
Return:
*/
template <typename T>
void LinkedList<T>::addData(T newData)
{
LinkNode<T> *newNode = new LinkNode<T>;
newNode->setData(newData);
LinkNode<T> *currPtr;
LinkNode<T> *prePtr;
prePtr = head;
currPtr = head->getNext();
switch (sort)
{
case 0: //if unsorted
{
while (currPtr != nullptr) //goes to end of the list
{
prePtr = currPtr;
currPtr = currPtr->getNext();
}
break;
}
case 1: //ascending order
{
while (currPtr != nullptr && newNode->getData() > currPtr->getData()) //newNode will be between smaller and larger value
{
prePtr = currPtr;
currPtr = currPtr->getNext();
}
break;
}
case 2: //descending order
{
while (currPtr != nullptr && newNode->getData() < currPtr->getData()) //newNode will be between larger and smaller value
{
prePtr = currPtr;
currPtr = currPtr->getNext();
}
break;
}
default:
break;
}
prePtr->setNext(newNode);
newNode->setNext(currPtr);
++count; //increment the count
if (newNode->getNext() == nullptr)
tail = newNode;
//setTail(); not needed but may be useful later
}
/* adds data to unsorted list by position. Returns -1 if the list is sorted because this operation would not be allowed.
Pre: newData - T data
position - int index of newNode
Post:
Return: int - position. returns -1 if this operation fails.
*/
template <typename T>
int LinkedList<T>::addData(T newData, int position)
{
if (sort != 0) //if sort type is not unsorted
return -1;
else if (position < 0 || position > count) //if position out of bounds
{
return -1;
}
LinkNode<T> *newNode;
newNode = new LinkNode<T>;
newNode->setData(newData);
LinkNode<T> *currPtr;
LinkNode<T> *prePtr;
prePtr = head;
currPtr = head->getNext();
int index = 0;
while (currPtr != nullptr && index < position) //goes to index
{
prePtr = currPtr;
currPtr = currPtr->getNext();
++index;
} //currPtr is now on index position
prePtr->setNext(newNode);
newNode->setNext(currPtr); //inserted behind index position, becomes the new index position
++count;
if (newNode->getNext() == nullptr)
tail = newNode;
//setTail(); //not needed but may be useful later
return position;
}
/* deletes one node of delData value
Pre: delData - T data to be deleted
Post:
Return: true or false. false if data not found in list. true otherwise
*/
template <typename T>
bool LinkedList<T>::deleteData(T delData)
{
bool deleted = false;
LinkNode<T> *currPtr;
LinkNode<T> *prePtr;
prePtr = head;
currPtr = head->getNext();
while (currPtr != nullptr && currPtr->getData() != delData)
{
prePtr = currPtr;
currPtr = currPtr->getNext();
}
if (currPtr != nullptr)
{
deleted = true;
prePtr->setNext(currPtr->getNext());
delete currPtr;
--count;
}
if (!isEmpty() && prePtr->getNext() == nullptr)
tail = prePtr;
return deleted;
}
/* empies the list out except for the head.
Pre:
Post: list is now empty. head points to null
Return:
*/
template <typename T>
void LinkedList<T>::emptyList()
{
LinkNode<T> *currPtr = head->getNext();
LinkNode<T> *nextPtr;
while (currPtr != nullptr)
{
nextPtr = currPtr->getNext();
delete currPtr;
currPtr = nextPtr;
--count;
}
head->setNext(nullptr);
tail = nullptr;
}
/* looks for data in the linked list.
Pre: search - T
Post:
Return: int - returns the index of the search value node
*/
template <typename T>
int LinkedList<T>::findData(T search)
{
int notFoundIndex = -1;
int index = 0;
LinkNode<T> *currPtr;
currPtr = head->getNext();
while (currPtr != nullptr)
{
if (currPtr->getData() == search)
{
return index;
}
currPtr = currPtr->getNext();
++index;
}
return notFoundIndex;
}
/*
Pre:
Post:
Return: int - number of nodes in a list
*/
template <typename T>
int LinkedList<T>::getCount() const
{
return count;
}
/*
Pre:
Post:
Return: int - type of order
*/
template <typename T>
int LinkedList<T>::getSort() const
{
return sort;
}
/*
Pre:
Post:
Return: bool - whether if it is empty
*/
template <typename T>
bool LinkedList<T>::isEmpty()
{
if (count == 0)
return true;
else
return false;
}
/* finds the tail of the list. not always needed (more efficient ways implemented in the program)
Pre:
Post: sets tail member to the rear of the list
Return:
*/
template <typename T>
void LinkedList<T>::setTail()
{
LinkNode<T> *currPtr;
currPtr = head->getNext();
while (currPtr->getNext() != nullptr)
{
currPtr = currPtr->getNext();
}
tail = currPtr;
}
/* gets the tail
Pre:
Post:
Return: T - the rear of the list. modifiable if needed.
*/
template <typename T>
T& LinkedList<T>::getTail()
{
if (tail != nullptr)
{
return tail->getData();
}
else
return head->getData();
}
#endif