summaryrefslogtreecommitdiffstats
path: root/linked_list.c
blob: a9d308999c92cc31ead4e4d14e36db7bfa483218 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node
{
    int id;
    struct Node *next;
};
typedef struct Node Node_t;

Node_t *head = NULL;
Node_t *tail = NULL;

int main(void)
{
    int i;
    for (i = 0; i < 10; i++)
    {
        if (head == NULL)
        {
            printf("head, adding id: %d\n", i);
            head = (Node_t *) malloc(sizeof(Node_t));
            head->id = i;
            head->next = NULL;
            tail = head;
        }
        else
        {
            printf("tail, adding id: %d\n", i);
            Node_t *tmp = (Node_t *) malloc(sizeof(Node_t));
            tmp->id = i;
            tmp->next = NULL;
            head->next = tmp;
            head = head->next;
        }
    }

    while (tail != NULL)
    {
        printf("tail: %d\n", tail->id);
        tail = tail->next;
    }

    return 0;
}