A Linked List is a collection of nodes where each node contains data and a pointer to the next node.
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Dynamic size (no fixed size)
- Efficient insertion and deletion
- No memory wastage
A Doubly Linked List is a linked list where each node contains data, previous pointer, and next pointer.
A Circular Linked List is a linked list in which the last node points back to the first node instead of NULL.
A self-referential structure is a structure that contains a pointer to itself.
struct Node
{
int data;
struct Node *next;
};
It is the process of allocating memory for nodes at runtime using functions like malloc().
Traversal is the process of visiting each node in a linked list one by one.
- At beginning → O(1)
- At end or middle → O(n)
Singly Linked List is a collection of nodes containing data and next pointer.
STRUCTURE
HEAD → [10|→] → [20|→] → [30|→] → [NULL]
TRAVERSAL
Start from HEAD
Visit node
Move to next until NULL
INSERTION
At Beginning
new->next = head
head = new
At End
Traverse to last
last->next = new
At Middle
new->next = temp->next
temp->next = new
DELETION
From Beginning
head = head->next
From End
second_last->next = NULL
From Middle
prev->next = curr->next
Each node contains prev, data, next
NULL ← [A] ↔ [B] ↔ [C] → NULL
Insertion at Beginning
new->next = head
head->prev = new
head = new
Deletion from Middle
temp->prev->next = temp->next
temp->next->prev = temp->prev
Supports forward and backward traversal
Last node points to first node
HEAD → [10] → [20] → [30] ↑__________________________
Applications
- CPU scheduling
- Games
- Traffic systems
| Feature | Linked List | Array |
|---|---|---|
| Memory | Dynamic | Fixed |
| Access | Sequential | Random |
| Insertion | Easy | Difficult |
| Deletion | Easy | Difficult |
| Speed | Slow | Fast |
Insertion at Beginning
new->next = head
head = new
Deletion from End
second_last->next = NULL
| Operation | Time |
|---|---|
| Traversal | O(n) |
| Insertion | O(1) |
| Deletion | O(n) |
| Search | O(n) |
Linked List → Fast insertion, Slow searching
0 Comments