LinkedLists MCH
LinkedLists MCH
1 2 3 4 5 6 7 X
•Every node contains two parts- one data and the other a pointer
to the next node.
•The left part of the node i.e. data part may be 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.
INTRODUCTION contd.
HEAD pointer stores the address of the first node in the list.
The next part of the first node store the address of its succeeding
node.
HEAD = NULL means that the linked list is empty and contains no
nodes.
AVAIL is a pointer to the first free space available in memory,
maintained by operating system.
In C, node of a linked list can be defined as follows:
struct node
{
int data;
struct node *next;
};
Singly Linked List
Simplest type of linked list
Every node contains some data and a pointer to the next node of the
same data type i.e. it stores the address of the next node in sequence.
HEAD
1 2 3 4 5 6 7 X
HEAD , New_Node
9 1 7 3 4 2 6 5 X
HEAD, PTR
1 7 3 4 2 6 5 9 X
1 7 3 4 2 6 X
HEAD, PTR
1 7 3 4 2 6 5 X
HEAD PTR
1 7 3 9 4 2 6 5 X
HEAD, PTR
7 3 4 2 6 5 X
HEAD
1 7 3 4 2 6 X 5 X
Step 1:
IF HEAD = NULL, then
Step 2: Write UNDERFLOW
Step 3: Go to Step 11
Step 4:
[END OF IF]
Step 5:
SET PTR = HEAD
Step 6:
Repeat Step 7 and 8 while PTR->NEXT ≠ NULL
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PREPTR->NEXT = NULL
Step 10: FREE PTR
Step 11: EXIT
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
1 7 3 4 6 5 X
HEAD PREPTR
X 1 1 2 3 4 X
X 1 7 3 4 2 X
HEAD
X 9 1 7 3 4 2 X
HEAD, New_Node
Algorithm to insert a new node at the end of the doubly linked list
X 1 7 3 4 2 X
HEAD, PTR
X 1 7 3 4 2 9 X
X 1 7 3 4 2 X
HEAD, PTR
X 1 7 3 4 2 X
HEAD PTR
9 X 1 7 3 9 4 2 X
HEAD
Algorithm to delete the first node from the doubly linked list
X 1 7 3 4 2 X
HEAD, PTR
7 3 4 2 X
HEAD
Algorithm to delete the last node of the doubly linked list
X 1 3 5 7 8 9 X
HEAD, PTR
X 1 3 5 7 8 9 X
HEAD PTR
X 1 3 5 7 8 X
HEAD
Algorithm to delete an intermediate node from the doubly linked list
X 1 3 4 7 8 9 X
HEAD, PTR
X 1 3 4 7 8 9 X
PTR
HEAD
X 1 3 4 8 9 X
HEAD
Circular Linked List
Last node contains a pointer to the first node of the list.
HEAD
1 2 3 4 5 6 7
Algorithm to insert a new node in the beginning of circular linked list
1 7 3 4 2 6 5
HEAD, PTR
1 7 3 4 2 6 5
HEAD PTR
9 1 7 3 4 2 6 5
HEAD, New_Node
Algorithm to insert a new node at the end of the circular linked list
1 7 3 4 2 6 5
HEAD, PTR
1 7 3 4 2 6 5 9
New_Node
HEAD PTR
Algorithm to insert a new node after a node that has value NUM
1 7 3 4 2 6 5
HEAD, PTR
1 7 3 4 2 6 5
HEAD PTR
9 1 7 3 9 4 2 6 5
New_Node HEAD
Algorithm to delete the first node from the circular linked list
1 7 3 4 2 6 5
HEAD, PTR
1 7 3 4 2 6 5
HEAD PTR
7 3 4 2 6 5
HEAD
Algorithm to delete the last node of the circular linked list
1 7 3 4 2 6 5
HEAD, PREPTR, PTR
1 7 3 4 2 6 5
HEAD PREPTR PTR
1 7 3 4 2 6
HEAD
Algorithm to delete a given node from the circular linked list
Step 1: IF HEAD = NULL, then
Step 2: Write UNDERFLOW
Step 3: Go to Step 11
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 while PTR->DATA ≠ NUM
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: Repeat Step 10 while PREPTR->NEXT ≠ PTR
Step 10: SET PREPTR = PREPTR->NEXT
[END OF LOOP]
Step 11: SET PREPTR->NEXT = PTR->NEXT
Step 12: SET PTR->NEXT = NULL
Step 13: FREE PTR
Step 14: EXIT
1 7 3 4 2 6 5
HEAD, PREPTR, PTR
1 7 3 4 2 6 5
HEAD PREPTR PTR
1 7 3 4 6 5
HEAD
A circular two way linked list which contains a pointer to the next as well as
previous node in the sequence.
The next field of the last node stores the address of the first node of the list,
i.e., HEAD. Similarly, the previous field of the first node stores the address of
the last node.
Contains three parts in its structure, it calls for more space per node and for
more expensive basic operations. However, it provides the ease to
manipulate the elements of the list as it maintains pointers to nodes in both
the directions . The main advantage of using a circular doubly linked list is
that it makes searches twice as efficient.
HEAD
1 1 2 3 4
Array representation of linked lists
2 single dimensional arrays (or 1-D arrays) are required to represent a singly LL containing
only one data field.
HEAD 1 7 3 4 2 6 5 X
0 1 2 3 4 5 6
3 4 1 2 7 5 6
Address (or Index) Array (hold index of next element in sequence)
1 3 4 6 0 -1 5
Start: 2 (index)
For insertion and deletion operations, both the arrays need to be changed accordingly.
More number of arrays will be required as the data fields increase in number.
For a doubly linked list, at least 3 such 1-D arrays will be required, one for holding data part, one
for holding index of next data, and the other for holding index of previous data.
Applications