From 709af8ba56169f280a0f25b8fd4739a8bbe9df89 Mon Sep 17 00:00:00 2001 From: ch2br58 <116263409+ch2br58@users.noreply.github.com> Date: Thu, 20 Oct 2022 10:42:00 +0100 Subject: [PATCH] Create linklist.c --- LinkedList/linklist.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 LinkedList/linklist.c diff --git a/LinkedList/linklist.c b/LinkedList/linklist.c new file mode 100644 index 0000000..59b5444 --- /dev/null +++ b/LinkedList/linklist.c @@ -0,0 +1,45 @@ +// Linked list implementation in C + +#include +#include + +// Creating a node +struct node { + int value; + struct node *next; +}; + +// print the linked list value +void printLinkedlist(struct node *p) { + while (p != NULL) { + printf("%d ", p->value); + p = p->next; + } +} + +int main() { + // Initialize nodes + struct node *head; + struct node *one = NULL; + struct node *two = NULL; + struct node *three = NULL; + + // Allocate memory + one = malloc(sizeof(struct node)); + two = malloc(sizeof(struct node)); + three = malloc(sizeof(struct node)); + + // Assign value values + one->value = 1; + two->value = 2; + three->value = 3; + + // Connect nodes + one->next = two; + two->next = three; + three->next = NULL; + + // printing node-value + head = one; + printLinkedlist(head); +}