From 566ae2c7e8e9012b371be6a0110c4212bee27070 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 1 Aug 2020 20:48:36 +0700 Subject: [PATCH] Add traverse and add some logic for linked list --- tests/linkedlist.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/linkedlist.c b/tests/linkedlist.c index 8a278ba..aca7fc6 100644 --- a/tests/linkedlist.c +++ b/tests/linkedlist.c @@ -68,6 +68,54 @@ void add_first(int element) { return; } + // put the first element of the head + // because we need the old head into the next of new head node->next = head; + + // replace the head node from current node head = node; +} + +void add_last(int element) { + struct node *node, *temp; + + node = (struct node*)malloc(sizeof (struct node)); + node->data = element; + count++; + + if (head == NULL) { + head = node; + head->next = NULL; + return; + } + + // store current as tempo + temp = node; + + while (temp->next != NULL) { + temp = temp->next; + } + + temp->next = node; + node->next = NULL; +} + +void traverse() { + struct node *node; + + node = head; + + if (node == NULL) { + printf("Linked list is empty!\n"); + return; + } + + printf("Total elements count: %d in linked list.\n", count); + + while (node->next != NULL) { + printf("%d\n", node->data); + node = node->next; + } + + printf("%d\n", node->data); } \ No newline at end of file