-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.c
150 lines (127 loc) · 2.76 KB
/
LinkedList.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
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
#include <stdio.h>
#include "LinkedList.h"
/*
* @Author: Hannes de Waal (c) 2006
*
* @Description:
* Logger module
*
* @log:
* 18 May 2004 - version 1.0
*
*
*/
LinkedList_t *
LinkedList_new ()
{
LinkedList_t *new = (LinkedList_t *) malloc (sizeof (LinkedList_t));
new->head = NULL;
new->iter = NULL;
return new;
}
void
insert (LinkedList_t * l, void *value)
{
/* The node that should precede the new node in the list. */
Node *previous = NULL;
/* The node we are currently comparing to our new value */
Node *current = l->head;
/* The new node */
Node *new = newNode (value);
/* Find the nodes that should precede and follow the new node. */
//while (current != NULL && current->value < value) {
while (current != NULL)
{
previous = current;
current = current->next;
}
/* Special case the situation where we are adding the first
node to the list */
if (previous == NULL)
{
l->head = new;
}
else
{
previous->next = new;
}
/* Set the node that follows the new node. */
new->next = current;
}
int
removeValue (LinkedList_t * l, void *value)
{
/* The node that precedes the node we are removing */
Node *previous = NULL;
/* The node we are examining */
Node *current = l->head;
/* Keep looking until there are no more nodes or we have found
or gone past the value we are looking for. */
//while (current != NULL && current->value < value) {
while (current != NULL)
{
previous = current;
current = current->next;
}
/* If we reached the end of the list or we found a value
higher than what we wanted, then the value is not in the
list */
//if (current == NULL || current->value > value) {
if (current == NULL)
{
return -1;
}
if (previous == NULL)
{
/* Removing the head of the list. */
l->head = current->next;
}
else
{
/* Remove the current element by having its predecessor
skip it. */
previous->next = current->next;
}
/* Free the memory associated with the node we just removed. */
free (current);
return 0;
}
void
LinkedList_loop (LinkedList_t * l, int (*fncb) (void *))
{
Node *current;
for (current = l->head; current != NULL; current = current->next)
{
fncb (current->value);
}
}
void
LinkedList_free (LinkedList_t * l)
{
Node *current;
for (current = l->head; current != NULL; current = current->next)
{
free (current);
}
free (l);
}
Node *
newNode (void *value)
{
Node *new = (Node*) malloc (sizeof (Node));
new->value = value;
new->next = NULL;
return new;
}
#if defined TEST
int
main ()
{
LinkedList *l;
char *d = { "h" };
l = new_LinkedList ();
insert (l, d);
removeValue (l, d);
printf ("%s\n", d);
}
#endif