-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
261 lines (216 loc) · 7.19 KB
/
Copy pathList.cpp
File metadata and controls
261 lines (216 loc) · 7.19 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
/* Implementation of the Link class, which represents a linked list of data to form a coherent data structure. Defines various member functions
that allow for manipulation and transversal of the linked list.
Author: Ethan Smith
CPSC 1070 - Spring 2024
Lab 06 Phase C
*/
#include "List.h"
void List::insertStart(int data) {
Node* newNode = new Node(data); // Creates new node
newNode->setNextNode(head); // Sets the next node of the new node to the current head
head = newNode; // Updates
}
void List::insertEnd(int data) {
Node* newNode = new Node(data);
// If list is empty, set new node as head and return
if (head == nullptr) {
head = newNode;
return;
}
// Traverse the list to find the last node
Node* current = head;
while (current->getNextNode() != nullptr) {
current = current->getNextNode();
}
// Return the last node
current->setNextNode(newNode);
}
void List::insertAt(int data, int addr) {
Node* newNode = new Node(data);
// If address = 0, insert new node at the start of the list
if (addr == 0) {
newNode->setNextNode(head);
head = newNode;
return;
}
// Traverse the list to find the node at the specified point
Node* current = head;
for (int i = 0; i < addr - 1 && current != nullptr; i++) {
current = current->getNextNode();
}
// If out of range, return error message
if (current == nullptr) {
cerr << "Error: Address out of range\n";
delete newNode;
return;
}
// Insert new node after the node at the specified point
newNode->setNextNode(current->getNextNode());
current->setNextNode(newNode);
}
void List::removeEnd(int* data) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: List is empty\n";
return;
}
// If only one node in list
if (head->getNextNode() == nullptr) {
*data = head->getData(); // Set data to the value of head node
delete head; // Delete head node
head = nullptr;
return;
}
// Traverse the list to find second-to-last node
Node* current = head;
while (current->getNextNode()->getNextNode() != nullptr) {
current = current->getNextNode();
}
// Sets data to the value of the last node, deletes the last node, and sets the next pointer of second-to-last node to nullptr
*data = current->getNextNode()->getData();
delete current->getNextNode();
current->setNextNode(nullptr);
}
void List::removeStart(int* data) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: The list is empty\n";
return;
}
*data = head->getData(); // Gets the data from the head node
Node* temp = head; // Stores the address of the head node in a temporary pointer
head = head->getNextNode(); // Updates the head pointer to point to the next node
delete temp; // Deletes the original head node
// If the list becomes empty, output
if (head == nullptr) {
cerr << "The list is now empty\n";
}
}
void List::removeAt(int* data, int addr) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: The list is empty\n";
return;
}
// If specified point is 0, remove first node
if (addr == 0) {
*data = head->getData();
Node* temp = head;
head = head->getNextNode();
delete temp;
return;
}
// Traverse list to find specified node point
Node* current = head;
for (int i = 0; i < addr - 1 && current != nullptr; i++) {
current = current->getNextNode();
}
// If specified point given is out of range, return error
if (current == nullptr || current->getNextNode() == nullptr) {
cerr << "Error: Address out of range\n";
return;
}
// Remove the node at point
Node* temp = current->getNextNode();
*data = temp->getData(); // Gets data from the node to be removed
current->setNextNode(temp->getNextNode()); // Updates the next pointer of the previous node
delete temp;
}
void List::swapThisAndNext(int addr) {
// Traverse list to find specified node point
Node* current = head;
for (int i = 0; i < addr && current != nullptr; i++) {
current = current->getNextNode();
}
// If at the end, do nothing
if (current == nullptr || current->getNextNode() == nullptr) {
return;
}
// Swap the data of current node to the next node according to pointers
Node* nextNode = current->getNextNode();
int temp = current->getData();
current->setData(nextNode->getData());
nextNode->setData(temp);
}
int List::getFirst(int * data) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: List is empty\n";
return -1;
}
// Return head data
*data = head->getData();
return *data;
}
int List::getLast(int * data) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: List is empty\n";
return -1;
}
// Traverse to the end of the list
Node* current = head;
while (current->getNextNode() != nullptr) {
current = current->getNextNode();
}
// Get data of last node and return
*data = current->getData();
return *data;
}
int List::getAt(int position) {
// If list is empty, return error
if (head == nullptr) {
cerr << "Error: List is empty\n";
return -1;
}
// Traverse list to specified point
Node* current = head;
int currentPosition = 0;
while (current != nullptr && currentPosition < position) {
current = current->getNextNode();
currentPosition++; // Update position until reaching specified pos
}
// If position mathces the desired location, return data. Else, position is out of range, return error
if (currentPosition == position && current != nullptr) {
return current->getData();
} else {
cerr << "Error: Position out of range\n";
return -1;
}
}
int List::find(int value) {
Node* current = head;
int position = 0;
// Traverse the list
while (current != nullptr) {
if (current->getData() == value) { // If current node matches desired value, return position in list
return position;
}
// If not, continue iterating through the list while incrementing position each time
current = current->getNextNode();
position++;
}
return -1; // Return if not found in list
}
int List::size() {
int count = 0;
Node* current = head; // Sets current to head node
// Counts through the list and sets count
while (current != nullptr) {
count++;
current = current->getNextNode();
}
return count;
}
bool List::empty() {
return head == nullptr; // Checks if head is null; Empty list
}
void List::printList() { // Added function
Node* current = head; // Starts at the head node
// Traverses list and outputs data from each position followed by a space until reaching the end.
while (current != nullptr) {
cout << current->getData() << " ";
current = current->getNextNode();
}
cout << endl; // Newline
}