-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list.c
122 lines (110 loc) · 2.74 KB
/
linked-list.c
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct Node {
struct Node * next;
int val;
};
struct List {
struct Node* head;
int size;
};
struct List* createlist(){
struct List* newlist = (struct List*) malloc(sizeof(struct List));
newlist->head = NULL;
newlist->size = 0;
return newlist;
}
struct Node* findprev(struct List* list, int val){
struct Node* prev = NULL;
if(!list) return prev;
struct Node* cur = list->head;
while(cur){
if(val < cur->val) {
/* the value to insert goes before cur node */
break;
}
else{
/* continue moving through the list */
prev = cur;
cur = cur->next;
}
}
return prev;
}
void addvalue(struct List* list, int val){
if(!list) return;
/* construct and initialize new node */
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node*));
printf("malloc: %p\n", newnode);
newnode->val = val;
newnode->next = NULL;
/* locate the node in the current list that will proceed newnode */
struct Node* prev = findprev(list, val);
if(prev){
/* the new node does not belong at the head of list */
/* option to eliminate duplicates - uncomment next line */
/* if(prev->val == val) return; */
newnode->next = prev->next;
prev->next = newnode;
}else{
/* the new node belongs at head of list */
newnode->next = list->head;
list->head = newnode;
}
list->size += 1;
}
void removevalue(struct List* list, int val){
if(!list || !list->head) return;
struct Node* cur = list->head;
struct Node* prev = NULL;
while(cur){
if(cur->val == val){
if(prev){
prev->next = cur->next;
free(cur);
}else{
/* node to remove is head */
list->head = list->head->next;
free(cur);
}
list->size -= 1;
break;
}else{
prev = cur;
cur = cur->next;
}
}
}
void printlist(struct List* list){
if(!list) return;
printf("list size: %i\n", list->size);
struct Node* cur = list->head;
printf("head -> ");
while(cur){
printf("%02i -> ", cur->val);
cur = cur->next;
}
printf("NULL\n");
}
int main() {
struct List* l = createlist();
addvalue(l, 1);
addvalue(l, 3);
addvalue(l, 4);
addvalue(l, 2);
srand(time(NULL));
for(int i = 0; i<100; i++){
if(rand()%2){
addvalue(l,i);
}
}
printlist(l);
for(int i = 0; i<100; i++){
if(rand()%2){
removevalue(l,i);
}
}
printlist(l);
return 0;
}