Linked List
Linked List
C, 2e
Reema Thareja
Linked Lists
1 2 3 4 5 6 7 X
• In the above linked list, every node contains two parts - one
integer and the other a pointer to the next node.
• The left part of the node which contains data may include a
simple data type, an array or a structure.
• The right part of the node contains a pointer to the next node (or
address of the next node in sequence).
• The last node will have no next node connected to it, so it will
store a special value called NULL.
© Oxford University Press 2014. All rights
Traversing Linked Lists
• We can traverse the entire linked list using a single pointer
variable called START.
• The START node contains the address of the first node; the next
part of the first node in turn stores the address of its succeeding
node.
• Using this technique the individual nodes of the list will form a
chain of nodes.
• If START = NULL, this means that the linked list is empty and
contains no nodes.
• In C, we can implement a linked list using the following code:
struct node
{
int data;
struct node *next;
};
© Oxford University Press 2014. All rights
START Pointer
START
NEXT
1 DATA
1 H 4
2
START pointing to
the first element of 3
the linked list in 4 E 7
memory 5
AVAIL 6
9 7 L 8
L 10
8
9
10 O -1
START
1 2 3 4 5 6 7 X
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
START
9 1 7 3 4 2 6 5 X
START
START, PTR
1 7 3 4 2 6 5 9 X
START
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM
1 7 3 4 2 6 5 X
START
7 3 4 2 6 5 X
START
1 7 3 4 2 6 5 X
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
© Oxford University Press 2014. All rights
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
START
1 2 3 4 5 6 7
Algorithm to insert a new node in the beginning of circular the linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
9 1 7 3 4 2 6 5
START
Algorithm to insert a new node at the end of the circular linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
START PTR
Algorithm to insert a new node after a node that has value NUM
Algorithm to delete the first node from the circular linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
7 3 4 2 6 5
START
1 7 3 4 2 6 5
1 7 3 4 2 6 5
PREPTR
PTR
START
1 7 3 4 2 6
START
Algorithm to delete the node after a given node from the circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 6 5
START
START
X 1 1 2 3 4 X
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to
traverse the list in the backward direction as well.
X 1 7 3 4 2 X
START
X 9 1 7 3 4 2 X
START
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 9 X
PTR
© Oxford University Press 2014. All rights
Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 X
START PTR
9
X 1 7 3 9 4 2 X
START
X 1 7 3 4 2 X
START, PTR
7 3 4 2 X
3 9
X 1 5 7 8 1 X
START, PTR
3 9
X 1 5 7 8 1 X
START PTR
3
X 1 5 7 8 X
START
Algorithm to delete the node after a given node from the doubly linked list
3 9
X 1 4 7 8 1 X
START, PTR
3 9
X 1 4 7 8 1 X
START
PTR
3
X 1 4 8 9 X
START
• The difference between a doubly linked and a circular doubly linked list is same as
that exists between a singly linked list and a circular linked list. The circular doubly
linked list does not contain NULL in the previous field of the first node and the
next field of the last node. Rather, the next field of the last node stores the
address of the first node of the list, i.e; START. Similarly, the previous field of the
first field stores the address of the last node.
START
1 1 2 3 4
1 7 3 4 2
START
9 1 7 3 4 2
START
1 7 3 4 2
START
1 7 3 4 2 9
START
Algorithm to insert a new node after a node that has value NUM
1 7 3 4 2
START, PTR
1 7 3 4 2
PTR
START 9
1 7 3 9 4 2
START
Algorithm to delete the first node from the circular doubly linked list
3 9
1 5 7 8 1
START, PTR
3 9
5 7 8 1
START
3 9
1 5 7 8 1
START PTR
3
X 1 5 7 8
START
Algorithm to delete the node after a given node from the circular doubly
linked list
3 9
1 5 7 8 1
START
3 9
1 5 7 8 1
START PTR
3
1 4 8 9
START
Grounded header linked list which stores NULL in the next field of the last node
Circular header linked list which stores the address of the header node in the
next field of the last node. Here, the header node will denote the end of the list.
Header Node
4
1 2 3 5 6 X
START
Header Node
4
1 2 3 5 6
START