Exploring Singly Linked Lists in C
Imagine you have a chain of colourful beads, each connected to the next one. That’s how a linked list works in programming!
What’s a Singly Linked List?
A linked list is like a chain of boxes, and each box holds some treasure (we call it “data”). The cool thing is, that each box also knows where the next box in the chain is.
In C, we can create a linked list using structures. Each box in our chain will be a structure, and it will have two parts: the data
part where we keep our treasure, and the next
part where we remember where the next box is.
struct Node {
int data;
struct Node* next;
};
Here, we define our structure Node
with two parts: data
(to store our treasure, which is an integer) and next
(to point to the next box in the chain).
Let’s Build a Linked List!
Now, let’s create a linked list with three boxes:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main()
{
/* Creating three boxes */
struct Node* head = malloc(sizeof(struct Node));
struct Node* second = malloc(sizeof(struct Node));
struct Node* third = malloc(sizeof(struct Node));
/* Putting treasures in the boxes */
head->data = 1;
second->data = 2;
third->data = 3;
/* Linking the boxes together */
head->next = second;
second->next = third;
third->next = NULL;
/* Exploring the linked list */
struct Node* current = head;
while (current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
return 0;
}
Here’s what’s happening step by step:
#include <stdio.h>
and#include <stdlib.h>
: We include these so we can useprintf
to print things andmalloc
to reserve memory for our boxes.- We define our structure
Node
. struct Node* head = malloc(sizeof(struct Node));
: This line creates our first box (calledhead
) and reserves enough memory for it.head->data = 1;
: Here, we put the treasure1
inside our first box.struct Node* second = malloc(sizeof(struct Node));
: We make our second box and reserve space for it.second->data = 2;
: We place the treasure2
in our second box.struct Node* third = malloc(sizeof(struct Node));
: We create our third box.third->data = 3;
: The treasure3
finds a home in our third box.- Linking the boxes together:
head->next = second;
: We tell our first box where the second box is.second->next = third;
: We inform the second box where the third box is.third->next = NULL;
: We let the third box know that there are no more boxes after it.
Exploring the linked list:
struct Node* current = head;
: We start with our first box.while (current != NULL) { ... }
: We keep doing this as long as we have boxes left.printf("%d ", current->data);
: We print the treasure inside the box.current = current->next;
: We move to the next box.
Using Our Linked List
When you run this program, it will print 1 2 3
. This means our linked list is working perfectly!
Keep in mind that linked lists are super handy in programming. They let us store and organize data in a flexible way. Keep exploring and having fun with linked lists! Who knows what amazing adventures you’ll embark on next? 🚀✨