-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_list.c
72 lines (62 loc) · 1.41 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 <stdlib.h>
#include "linked_list.h"
linked_list list_create() {
linked_list ll = malloc(sizeof(struct linked_list));
ll->head = NULL;
return ll;
}
void list_destroy(linked_list ll) {
if (ll != NULL) {
list_node n = ll->head;
while (n != NULL) {
list_node t = n;
n = n->next;
free(t);
}
ll->head = NULL;
free(ll);
}
}
uint32_t list_length(linked_list ll) {
if (ll != NULL && ll->head != NULL) {
uint32_t count = 0;
list_node n = ll->head;
while (n != NULL) {
n = n->next;
count++;
}
return count;
}
return 0;
}
list_node list_item_at_idx(linked_list ll, uint32_t idx) {
if (ll != NULL && ll->head != NULL) {
uint32_t i = 0;
list_node n = ll->head;
while (i != idx && n != NULL) {
n = n->next;
i++;
}
return n;
}
return NULL;
}
void list_append(linked_list ll, list_node n) {
if (ll != NULL && ll->head != NULL) {
list_node prev = ll->head;
while (prev->next != NULL) {
prev = prev->next;
}
prev->next = n;
} else if (ll != NULL) {
ll->head = n;
}
}
list_node list_node_create(uint32_t size) {
list_node n = calloc(1, size);
n->next = NULL;
return n;
}
void list_node_destroy(list_node n) {
free(n);
}