Exploring Singly Linked Lists in C

Mr. T
Dev Genius
Published in
3 min readOct 25, 2023

--

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:

  1. #include <stdio.h> and #include <stdlib.h>: We include these so we can use printf to print things and malloc to reserve memory for our boxes.
  2. We define our structure Node.
  3. struct Node* head = malloc(sizeof(struct Node));: This line creates our first box (called head) and reserves enough memory for it.
  4. head->data = 1;: Here, we put the treasure 1 inside our first box.
  5. struct Node* second = malloc(sizeof(struct Node));: We make our second box and reserve space for it.
  6. second->data = 2;: We place the treasure 2 in our second box.
  7. struct Node* third = malloc(sizeof(struct Node));: We create our third box.
  8. third->data = 3;: The treasure 3 finds a home in our third box.
  9. 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? 🚀✨

--

--

​Software Engineering Student - ALX. I write about Programming because I think most articles that leave programming out misrepresent life. ​