🔴 Advanced  ·  Lesson 38

Linked List

What is a linked list?

An array keeps its items packed together in memory with a fixed size. A linked list takes a different approach: it stores items in separate little blocks called nodes, and connects them using pointers. Each node points to the next one, forming a chain.

The big advantage is flexibility — a linked list can grow and shrink one node at a time while the program runs, without deciding a size in advance.

The node: the building block

A node is a struct with two parts: the data, and a pointer to the next node.

C Language
struct Node {
    int data;            // the value
    struct Node *next;   // address of the next node
};

The next pointer is what links one node to another. In the last node, next is set to NULL to say "the list ends here."

Building a small list

Let's create three nodes with malloc and link them: 10 → 20 → 30.

C Language
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };

int main() {
    struct Node *head = malloc(sizeof(struct Node));
    struct Node *second = malloc(sizeof(struct Node));
    struct Node *third = malloc(sizeof(struct Node));

    head->data = 10;   head->next = second;
    second->data = 20; second->next = third;
    third->data = 30;  third->next = NULL;   // end
    return 0;
}

Now head starts a chain: 10 points to 20, 20 points to 30, and 30 points to NULL.

Traversing to print

To read a linked list you start at head and follow next until you hit NULL.

C Language
struct Node *temp = head;
while (temp != NULL) {
    printf("%d -> ", temp->data);
    temp = temp->next;    // move to next node
}
printf("NULL\n");
Output:
10 -> 20 -> 30 -> NULL
💡 Use a temp pointer

Walk the list with a copy like temp, not head itself. If you move head, you lose the start of your list forever.

Inserting at the end

To add a node at the end, walk to the last node and point its next at the new node.

C Language
void append(struct Node *head, int value) {
    struct Node *n = malloc(sizeof(struct Node));
    n->data = value;
    n->next = NULL;

    struct Node *temp = head;
    while (temp->next != NULL)   // find the last node
        temp = temp->next;
    temp->next = n;             // link it on
}

This walks to the node whose next is NULL, then attaches the new node there.

Linked list vs array

PointArrayLinked list
SizeFixedGrows/shrinks at run time
MemoryContiguousScattered, linked by pointers
Access by indexInstantWalk from the start
Insert/delete in middleCostly (shifting)Cheap (relink pointers)

Common mistakes

  • Forgetting to set the last node's next to NULL, so traversal never stops.
  • Walking the list with head itself and losing the start.
  • Not checking malloc for NULL before using a new node.
  • Forgetting to free nodes, causing memory leaks.
🏋️ Practice

Build a list of 1 → 2 → 3, print it, then write a count function that traverses the list and returns how many nodes it has. Confirm it prints 3.

Summary

  • A linked list is a chain of nodes, each holding data and a next pointer.
  • The last node's next is NULL to mark the end.
  • Nodes are created with malloc so the list grows at run time.
  • Traverse by following next from head until NULL.
  • Lists beat arrays for flexible size and cheap inserts, but lack instant index access.

Frequently Asked Questions

What is a linked list in C?
A linked list is a chain of small blocks called nodes, where each node holds a value and a pointer to the next node. Unlike an array, the nodes are not stored side by side in memory — they are connected by pointers, so the list can grow and shrink easily at run time.
What is a node in a linked list?
A node is one element of the list. In C it is usually a struct containing two parts: the data you want to store, and a pointer (often called next) that holds the address of the following node. The last node's next is NULL to mark the end.
How is a linked list different from an array?
An array stores elements together in fixed, contiguous memory with instant index access, but its size is fixed. A linked list scatters nodes and links them with pointers, so it grows and shrinks freely and inserts cheaply, but you must walk from the start to reach an element.
Why do linked lists use malloc?
Because the number of nodes is usually not known in advance, each node is created at run time with malloc, which returns a pointer to fresh memory on the heap. This is what lets the list grow one node at a time as needed.
How do you reach the end of a linked list?
You start at the head node and keep following each node's next pointer until you reach a node whose next is NULL. That NULL marks the last node, so the traversal loop stops there.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.