summaryrefslogtreecommitdiff
path: root/anim/tools/list.c
blob: 99836c546698e99998fc97a0fec0ce5dc325fcfa (plain) (blame)
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
#include <stdlib.h>

typedef struct node {
    long value;
    struct node *next;
} node;

node *head;

void push(long value) {
    node *n = malloc(sizeof *n);
    n->value = value;
    n->next = head;
    head = n;
}

void reverse(void) {
    node *prev = NULL;
    node *cur = head;
    while (cur) {
        node *next = cur->next;
        cur->next = prev;
        prev = cur;
        cur = next;
    }
    head = prev;
}

void clear(void) {
    while (head) {
        node *n = head;
        head = n->next;
        free(n);
    }
}

int main(void) {
    push(7);
    push(12);
    push(9);
    reverse();
    clear();
    return 0;
}