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); +}