-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cpp
68 lines (43 loc) · 1.21 KB
/
List.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
#include "List.h"
void addNode(int value, ListNode* startNode) {
ListNode* last = startNode;
while (last->nextNode != nullptr) {
last = last->nextNode;
}
last->nextNode = (ListNode*) malloc(sizeof(ListNode));
last->nextNode->value = value;
last->nextNode->nextNode = nullptr;
}
ListNode* getNode(int value, ListNode* startNode) {
ListNode* last = startNode;
while (last->nextNode != nullptr) {
if (last->value == value) {
return last;
}
last = last->nextNode;
}
return nullptr;
}
void printNodes(ListNode* startNode) {
ListNode* last = startNode;
if (last == nullptr) return;
int i = 0;
while (last->nextNode != nullptr) {
printf("\nNode%d with value %d", i, last->value);
i++;
last = last->nextNode;
}
}
void removeLast(ListNode* startNode) {
if (startNode->nextNode == NULL) {
retval = startNode->value;
free(startNode);
}
ListNode* current = startNode;
while (current->nextNode->nextNode != NULL) {
current = current->nextNode;
}
retval = current->nextNode->value;
free(current->nextNode);
current->nextNode = NULL;
}