-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
52 lines (44 loc) · 899 Bytes
/
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
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
Node * cons(void *ptr, Node *list) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->ptr = ptr;
node->next = list;
return node;
}
Node * reverse(Node *head) {
Node *ret = NULL;
while (head != NULL) {
ret = cons(head->ptr, ret);
head = head->next;
}
return ret;
}
bool null_list(Node *head) {
return (head == NULL);
}
bool member_list(void *ptr, Node *head) {
while (head != NULL) {
if (ptr == head->ptr) return true;
head = head->next;
}
return false;
}
u64 length_list(Node *head) {
u64 len = 0;
while (head != NULL) {
head = head->next;
len++;
}
return len;
}
void free_list(Node *head, bool free_contents) {
Node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
if (free_contents) free(tmp->ptr);
free(tmp);
}
}