-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistGen.c
190 lines (154 loc) · 3.99 KB
/
listGen.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
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
/**************/
/* list.c */
/**************/
#include <stdio.h>
#include <stdlib.h>
#include "listGen.h"
//////////////////////////////////////////
// Init
// Aim: create new list
// Input: pointer to the list structure
// Output: TRUE if succeeded
//////////////////////////////////////////
BOOL L_init(LIST* pList)
{
if (pList == NULL) return False; // no list to initialize
pList->head.next = NULL;
return True;
}
/////////////////////////////////////////////////////////////////
// Insert
// Aim: add new node
// Input: pointer to the node BEFORE the place for the new one
// a value to be stored in the new node
// Output: pointer to the new node
/////////////////////////////////////////////////////////////////
NODE* L_insert(NODE* pNode, DATA Value)
{
NODE* tmp;
if (!pNode) return NULL;
tmp = (NODE*)malloc(sizeof(NODE)); // new node
if (tmp != NULL) {
tmp->key = Value;
tmp->next = pNode->next;
pNode->next = tmp;
}
return tmp;
}
//////////////////////////////////////////////////////////////
// Delete
// Aim: erase node
// Input: pointer to the node BEFORE the node to be deleted
// Output: TRUE if succeeded
//////////////////////////////////////////////////////////////
BOOL L_delete(NODE* pNode, void (*freeFunc)(void*))
{
NODE* tmp;
if (!pNode || !(tmp = pNode->next)) return False;
pNode->next = tmp->next;
if (freeFunc != NULL)
freeFunc(tmp->key);
free(tmp);
return True;
}
/////////////////////////////////////////////////////////
// Find
// Aim: search for a value
// Input: pointer to the node to start with
// a value to be found
// Output: pointer to the node containing the Value
/////////////////////////////////////////////////////////
const NODE* L_find(const NODE* pNode, DATA value, int(*compare)(const void*, const void*))
{
const NODE* temp = NULL;
if (!pNode) return NULL;
while (pNode != NULL)
{
if (compare(pNode->key, value) == 0)
{
temp = pNode;
break;
}
pNode = pNode->next;
}
return temp;
}
////////////////////////////////////////////////
// Free (additional function)
// Aim: free the list memory
// Input: pointer to the list structure
// Output: TRUE if succeeded
////////////////////////////////////////////////
BOOL L_free(LIST* pList, void (*freeFunc)(void*))
{
NODE* tmp;
if (!pList) return False;
tmp = &(pList->head);
BOOL res = True;
while (res)
{
res = L_delete(tmp, freeFunc);
}
return True;
}
////////////////////////////////////////////////
// Print (additional function)
// Aim: print the list content (assume the DATA is int)
// Input: pointer to the list structure
// Output: a number of the printed elements
////////////////////////////////////////////////
int L_print(const LIST* pList, void(*print)(const void*))
{
NODE* tmp;
int c = 0;
if (!pList) return 0;
printf("\n");
for (tmp = pList->head.next; tmp; tmp = tmp->next, c++)
print(tmp->key);
printf("\n");
return c;
}
/////////////////////////////////////////////////////////////////
// InsertSorted
// Aim: add new node in a sorted way
// Input: pointer to the node BEFORE the place for the new one
// a value to be stored in the new node
// Output: pointer to the new node
/////////////////////////////////////////////////////////////////
NODE* L_insertSorted(LIST* pList, DATA Value, int(*compare)(const void*, const void*)) {
if (pList == NULL) {
return NULL;
}
NODE* ptr = &pList->head;
if (ptr == NULL) {
return NULL;
}
NODE* nextPtr = ptr->next;
while (nextPtr != NULL) {
if (compare(nextPtr->key, Value) > 0)
{
break;
}
ptr = nextPtr;
nextPtr = nextPtr->next;
}
return L_insert(ptr, Value);
}
//////////////////////////////////////////////
// L_count
// Aim: count the number of elements in the list
// Input: pointer to the list structure
// Output: number of elements in the list
//////////////////////////////////////////////
int L_count(const LIST* pList) {
if (pList == NULL) {
return 0;
}
NODE* ptr = pList->head.next;
int count = 0;
while (ptr != NULL) {
count++;
ptr = ptr->next;
}
return count;
}