/
linked_list.c
71 lines (60 loc) · 1.08 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 <stdio.h>
struct node {
int value;
struct node *next;
};
#define NODE_SIZE sizeof(struct node)
void apply(struct node *head, void (*fn)(struct node *));
struct node *create_list(int num);
void print(struct node *item);
void free_list(struct node *item);
int main()
{
struct node *list = create_list(1024);
apply(list, &print);
free_list(list);
return 0;
}
void apply(struct node *head, void (*fn)(struct node *))
{
struct node *item = head;
while(item) {
fn(item);
item = item->next;
}
}
struct node *create_list(int num)
{
if(num > 0) {
int i;
struct node *head, *curr, *next;
head = calloc(1, NODE_SIZE);
head->value = 0;
curr = head;
for(i = 0; i < num; i++) {
next = calloc(1, NODE_SIZE);
next->value = i;
curr->next = next;
curr = next;
}
return head;
} else {
return NULL;
}
}
void print(struct node *item)
{
printf("%d\n", item->value);
if(item->next == NULL)
printf("No more nodes.\n");
}
void free_list(struct node *head)
{
struct node *tmp;
while(head) {
tmp = head;
head = head->next;
free(tmp);
}
}