-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
71 lines (58 loc) · 1.37 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
/*
It will return a boolean value indicating the inserting process is successful or not.
- list: The list to insert the node.
- node: The node to be inserted.
*/
bool insert_node(List* list, Node* node){
if (list == NULL || node == NULL)
return false;
// If the list is not initialized
if (list->head == NULL){
list->head = node;
list->is_empty = false;
return true;
}
// Iterates to the tail node
Node* curr = list->head;
while (curr->next)
curr = curr->next;
// Now the curr is the last node in list
curr->next = node;
return true;
}
/*
Print the list.
- list: The list to be printed.
*/
bool print_list(List* list){
Node* curr = list->head;
while (curr){
printf("%d\n", curr->val);
curr = curr->next;
}
return true;
}
/*
Remove a node in a list.
- list: The list to remove a node.
- num: The val of the node to be removed.
*/
Node* remove_node(List* list, int num){
if (list == NULL || num == NULL)
return NULL;
Node* curr = list->head;
Node* prev = NULL;
while (curr && curr->val != num){
prev = curr;
curr = curr->next;
}
// num is not found in list
if (!curr)
return NULL;
prev->next = curr->next;
return curr;
}