diff options
Diffstat (limited to 'linked_list.c')
-rw-r--r-- | linked_list.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/linked_list.c b/linked_list.c new file mode 100644 index 0000000..a9d3089 --- /dev/null +++ b/linked_list.c @@ -0,0 +1,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; +} + |