A linked list is a chain of nodes. Each node holds data and the address of the next node, so the elements need not be next to each other in memory.
Definition
A linked list is a linear collection of data elements called nodes, in which the order is given by links (pointers) and not by physical position. Each node has two parts: the data (info) field and the next (link) field, which holds the address of the next node. A pointer called START or HEAD holds the address of the first node, and the next field of the last node holds NULL, which marks the end. An empty list has HEAD = NULL.
Definition: Linked list. A linear data structure made of nodes, each containing data and a pointer to the next node, in which the order of elements is set by the pointers rather than by their position in memory.
Representation in C
A node is declared with a self-referential structure: struct node { int data; struct node next; }; New nodes are created at run time with malloc, which returns the address of a block of memory, and nodes are released with free. Since memory is taken only when needed, the list can grow and shrink while the program runs. In older languages without pointers, a linked list can also be held in two parallel arrays, INFO and LINK, with a list of free cells called the AVAIL* list.
Comparison with Arrays
An array has fixed size and stores elements contiguously, so access by index is O(1), but insertion and deletion need shifting, O(n). A linked list has dynamic size and stores nodes anywhere, so insertion and deletion at a known node are O(1), just by changing pointers, but there is no direct access: reaching the kth node takes O(k) steps. Each node also needs extra memory for its pointer. Linked lists suit data whose size changes and where elements are often inserted and deleted.
Types of Linked Lists
In a singly linked list, each node points to the next only. In a doubly linked list, each node points to both the next and the previous node. In a circular linked list, the last node points back to the first. There is also the circular doubly linked list, and a header linked list with a special first node that holds information such as the count.
Table 2.1: Array and linked list
| Feature | Array | Linked list |
|---|---|---|
| Size | Fixed | Grows and shrinks |
| Memory | Contiguous | Scattered nodes |
| Access to kth element | O(1) | O(k) |
| Insert / delete at a known place | O(n) | O(1) |
| Extra memory | None | One pointer per node |
Example 2.1: Building a list of three nodes
Problem: Create a singly linked list with values 10, 20, 30 and print it.
Solution:
- Allocate three nodes with malloc and set their data.
- Link them: first → second → third → NULL.
- Traverse from head, printing each data value until NULL.
Result: The output is 10 -> 20 -> 30 -> NULL.
#include <stdio.h>
#include <stdlib.h>
struct node { int data; struct node *next; };
struct node *make(int v) {
struct node *p = malloc(sizeof *p);
p->data = v; p->next = NULL; return p;
}
int main(void) {
struct node *head = make(10);
head->next = make(20);
head->next->next = make(30);
for (struct node *p = head; p; p = p->next)
printf("%d -> ", p->data);
printf("NULL\n");
return 0;
}
Output
10 -> 20 -> 30 -> NULL
Common mistakes
- Losing the head pointer, which loses the whole list.
- Forgetting to set the next field of the last node to NULL.
- Not freeing nodes that are removed, which leaks memory.
Key takeaways
- A node holds data and a pointer to the next node.
- HEAD points to the first node, and the last node points to NULL.
- Linked lists grow dynamically, but have no direct access.
- Types: singly, doubly, circular and header lists.
Practice questions
- What are the two parts of a node?
- Why is access to the kth element slow in a linked list?
- What does malloc do?
Answers to the practice questions
- The data field and the link (next) field.
- Because the list must be followed from the head through k nodes.
- It allocates a block of memory at run time and returns its address.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II
