-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_delete_matches_from_a_linkedList.c
More file actions
289 lines (247 loc) · 6.42 KB
/
05_delete_matches_from_a_linkedList.c
File metadata and controls
289 lines (247 loc) · 6.42 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node
{
int value;
struct node *next;
} Node;
void printList(Node *head);
Node *insertAtHead(Node *head, int newValue);
Node *insertAtTail(Node *head, int newValue);
Node *deleteAtHead(Node *head);
Node *deleteAtTail(Node *head);
int length(Node *head); // Find the LENGTH of a Linked List
int recursiveLength(Node *node); // Find the LENGTH of a Linked List recursively
bool isMember(Node *node, int findValue); // SEARCH for a MEMBER in a Linked List
int countMatches(Node *node, int findValue); // Find & COUNT Matches in a Linked List
void replaceMatches(Node *node, int findValue,
int replaceValue); // Find & REPLACE Matches in a Linked List;
Node *deleteFirstMatch(Node *head, int deleteValue,
bool *wasDeleted); // DELETE the first node to have a MATCHING value with the passed value
Node *deleteAllMatches(Node *head, int deleteValue,
int *numDeleted); // DELETE all nodes with a value MATCHING with the passed value
int main()
{
Node *myListHead;
myListHead = NULL;
myListHead = insertAtHead(myListHead, 7);
myListHead = insertAtHead(myListHead, 5);
myListHead = insertAtHead(myListHead, 3);
myListHead = insertAtTail(myListHead, 5);
myListHead = insertAtTail(myListHead, 10);
myListHead = insertAtTail(myListHead, 10);
myListHead = insertAtTail(myListHead, 10);
myListHead = insertAtTail(myListHead, 12);
myListHead = insertAtTail(myListHead, 14);
printf("\nList before DELETING Matches...\n");
printList(myListHead);
// bool deleted;
// myListHead = deleteFirstMatch(myListHead, 3, &deleted); // DELETE the first node to have a MATCHING value
// if (deleted)
// {
// printf("\nA node with value 3 was deleted!\n");
// printf("List after DELETING...\n");
// printList(myListHead);
// }
// else
// printf("\nA node with value 3 was not deleted!\n");
// myListHead = deleteFirstMatch(myListHead, 8, &deleted); // DELETE the first node to have a MATCHING value
// if (deleted)
// {
// printf("\nA node with value 8 was deleted!\n");
// printf("List after DELETING...\n");
// printList(myListHead);
// }
// else
// printf("\nA node with value 8 was not deleted!\n");
// myListHead = deleteFirstMatch(myListHead, 10, &deleted); // DELETE the first node to have a MATCHING value
// if (deleted)
// {
// printf("\nA node with value 10 was deleted!\n");
// printf("List after DELETING...\n");
// printList(myListHead);
// }
// else
// printf("\nA node with value 10 was not deleted!\n\n");
int numDeleted;
myListHead = deleteAllMatches(myListHead, 5, &numDeleted); // DELETE all nodes with a value MATCHING with the passed value
printf("\n%d nodes with value 5 was deleted!\n", numDeleted);
printf("List after DELETING...\n");
printList(myListHead);
myListHead = deleteAllMatches(myListHead, 10, &numDeleted); // DELETE all nodes with a value MATCHING with the passed value
printf("\n%d nodes with value 10 was deleted!\n", numDeleted);
printf("List after DELETING...\n");
printList(myListHead);
return (0);
}
Node *deleteFirstMatch(Node *head, int deleteValue, bool *wasDeleted) //== DELETE the first node to have a MATCHING value with the passed value
{
if (head == NULL)
{
*wasDeleted = false;
return (NULL);
}
if (head->value == deleteValue)
{
Node *temp = head->next;
free (head);
*wasDeleted = true;
return (temp);
}
Node *current = head->next;
Node *previous = head;
while (current != NULL)
{
if (current->value == deleteValue)
{
previous->next = current->next;
free (current);
*wasDeleted = true;
return (head);
}
previous = current;
current = current->next;
}
*wasDeleted = false;
return (head);
}
Node *deleteAllMatches(Node *head, int deleteValue, int *numDeleted) //== DELETE all nodes with a value MATCHING with the passed value
{
Node *current = head;
bool deleted = false;
*numDeleted = 0;
do
{
current = deleteFirstMatch(current, deleteValue, &deleted);
if (deleted)
*numDeleted = *numDeleted + 1;
} while (deleted);
return (current);
}
int length(Node *head) // Find the LENGTH of a Linked List
{
Node *current;
current = head;
int length = 0;
while (current != NULL)
{
length++;
current = current->next;
}
return (length);
}
int recursiveLength(Node *node) // Find the LENGTH of a Linked List recursively
{
if (node == NULL)
return (0);
else
return (1 + recursiveLength(node->next));
}
bool isMember(Node *node, int findValue) // SEARCH for a MEMBER in a Linked List
{
if (node == NULL)
return (false);
else if (node->value == findValue)
return (true);
else
return (isMember(node->next, findValue));
}
int countMatches(Node *node, int findValue) // Find and COUNT Matches in a Linked List
{
if (node == NULL)
return (0);
else if (node->value == findValue)
return (1 + isMember(node->next, findValue));
else
return (countMatches(node->next, findValue));
}
void replaceMatches(Node *node, int findValue, int replaceValue) // Find & REPLACE Matches in a Linked List;
{
if (node != NULL)
{
if (node->value == findValue)
node->value = replaceValue;
replaceMatches(node->next, findValue, replaceValue);
}
}
Node *insertAtHead(Node *head, int newValue)
{
Node *newNode = calloc(1, sizeof(Node));
newNode->value = newValue;
if (head == NULL)
return (newNode);
else
{
newNode->next = head;
return (newNode);
}
// OR //
// if (head != NULL)
// {
// newNode->next = head;
// return (newNode);
// }
// return (newNode);
}
Node *insertAtTail(Node *head, int newValue)
{
Node *newNode = calloc(1, sizeof(Node));
newNode->value = newValue;
if (head == NULL)
return (newNode);
else
{
Node *current = head;
while (current->next != NULL)
current = current->next;
current->next = newNode;
return (head);
}
}
Node *deleteAtHead(Node *head)
{
if (head == NULL)
return (NULL);
else
{
Node *toReturn = head->next;
free (head);
return (toReturn);
}
}
Node *deleteAtTail(Node *head)
{
if (head == NULL)
return (NULL);
else if (head->next == NULL)
{
free (head);
return (NULL);
}
else
{
Node *current = head;
Node *previous = NULL;
while (current->next != NULL)
{
previous = current;
current = current->next;
}
previous->next = NULL;
free (current);
return (head);
}
}
void printList(Node *head)
{
Node *current;
current = head;
int i = 0;
while (current != NULL)
{
printf("Node %d: %d\n", i, current->value);
i++;
current = current->next;
}
}