-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintList.cpp
105 lines (65 loc) · 1.46 KB
/
intList.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
#include <cstdlib>
#include "intList.hpp"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
int sizeOfList(IntList* head) {
if(head == NULL) {
return 0;
}
IntList* tmp = head;
int c = 0;
while(tmp != NULL) {
tmp = tmp->next;
c++;
}
return c;
}
// Add Value to the end List
IntList* addToList(IntList* head, int value) {
IntList* tmp = (IntList*) malloc(sizeof(IntList));
tmp->value = value;
tmp->next = NULL;
if(head == NULL) {
head = tmp;
return head;
}
IntList* p;
p = head;
while(p->next != NULL) {
p = p->next;
}
p->next = tmp;
return head;
}
IntList* deleteList(IntList* head) {
IntList* current;
current = head;
IntList* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
/* deref head_ref to affect the real head back
in the caller. */
head = NULL;
return head;
}
unsigned int getNode(IntList* head, int i) {
int listSize = sizeOfList(head);
// std::cout << listSize << " " << i << std::endl;
if(listSize <= i) {
std::cout << "Index out of bound - List Size " << listSize << " - index " << i << std::endl;
exit(0);
}
IntList* tmp = head;
int c = 0;
while(i-- >= 0 && tmp->next != NULL) {
tmp = tmp->next;
c++;
}
return tmp->value;
}