Introduction:
Linked lists are fundamental data structures widely used in computer science and programming. In this guide, we’ll walk through a simple implementation of a linked list in the C programming language. This explanation will be beginner-friendly, breaking down each line of code to understand how linked lists work and how to manipulate them.
Let’s dive in!
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 | #include <stdio.h> #include <stdlib.h> // Creating a node struct node { int value; struct node *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; head = one; // printing node-value struct node *current = head; while (current != NULL) { printf ( "%d " , current->value); // print current node's value current = current->next; // move to the next node } return 0; } |
Now, let’s break down the code step by step:
1 2 | #include <stdio.h> #include <stdlib.h> |
These lines include necessary header files. <stdio.h>
is for input and output operations like printf
, and <stdlib.h>
is for functions like malloc
, which is used to allocate memory dynamically.
1 2 3 4 | struct node { int value; struct node *next; }; |
This defines a structure named node
. Each node has an integer value (value
) and a pointer to another node (next
), which represents the next node in the linked list.
1 2 3 4 5 6 | int main() { // Initialize nodes struct node *head; struct node *one = NULL; struct node *two = NULL; struct node *three = NULL; |
Here, we’re declaring pointers to nodes. head
will point to the first node of the linked list, and one
, two
, and three
will point to individual nodes.
1 2 3 4 | // Allocate memory one = malloc ( sizeof ( struct node)); two = malloc ( sizeof ( struct node)); three = malloc ( sizeof ( struct node)); |
This allocates memory for each node using malloc
. It allocates memory equivalent to the size of the struct node
for each node pointer (one
, two
, and three
).
1 2 3 4 | // Assign value values one->value = 1; two->value = 2; three->value = 3; |
Here, we’re assigning values to the value
member of each node. one
gets 1
, two
gets 2
, and three
gets 3
.
1 2 3 4 | // Connect nodes one->next = two; two->next = three; three->next = NULL; |
This part links the nodes together. one->next
is assigned to two
, two->next
is assigned to three
, and three->next
is set to NULL
to indicate the end of the list.
1 | head = one; |
This sets the head
pointer to point to the first node of the linked list, which is one
.
1 2 3 4 5 6 7 | // printing node-value struct node *current = head; while (current != NULL) { printf ( "%d " , current->value); // print current node's value current = current->next; // move to the next node } |
Here, we’re traversing the linked list starting from head
. Inside the while
loop, we print the value
of the current node and then move to the next node by updating current
to point to the next node (current->next
).
Finally, we return 0
to indicate successful completion of the program.