05 Linked List
05 Linked List
GTU # 3130702
Unit-2
Linear Data Structure
(Linked List)
One method of obtaining the address of node is to store address in computer’s main memory,
we refer this addressing mode as pointer of link addressing.
A simple way to represent a linear list is to expand each node to contain a link or pointer to the
next node. This representation is called one-way chain or Singly Linked Linear List.
Linked Storage Representation
NULL
Last Node Value
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
A linked List
The linked allocation method of storage can result in both efficient use of computer storage and
computer time.
A linked list is a non-sequential collection of data items.
Each node is divided into two parts, the first part represents the information of the element and the second
part contains the address of the next mode.
The last node of the list does not have successor node, so null value is stored as the address.
It is possible for a list to have no nodes at all, such a list is called empty list.
Pros & Cons of Linked Allocation
Insertion Operation
We have an n elements in list and it is required to insert a new element between the first and second element,
what to do with sequential allocation & linked allocation?
Insertion operation is more efficient in Linked allocation.
X 1000
2100
Pros & Cons of Linked Allocation
Deletion Operation
Deletion operation is more efficient in Linked Allocation
Join Operation
▪ Join operation is more efficient in Linked Allocation.
Linked list require more memory compared to array because along with value it stores pointer
to next node.
Linked lists are among the simplest and most common data structures. They can be used to
implement other data structures like stacks, queues, and symbolic expressions, etc…
Operations & Type of Linked List
Operations on Linked List Types of Linked List
▪ Insert ▪ Singly Linked List
• Insert at first position ▪ Circular Linked List
• Insert at last position ▪ Doubly Linked List
• Insert into ordered list
▪ Delete
▪ Traverse list (Print list)
▪ Copy linked list
Singly Linked List
Info Link
Accessing Part
Nod
of Node
Typical Node Info (Node)
e
Data Pointer to Link (Node)
Next Node
struct node
{
C Structure to int info;
represent a node struct node *link;
};
Algorithms for singly linked list
1. Insert at first position
2. Insert at last position
3. Insert in Ordered Linked list
4. Delete Element
5. Copy Linked List
Availability Stack
A pool or list of free nodes, which we refer to as the availability stack is maintained in
conjunction with linked allocation.
Whenever a node is to be inserted in a list, a free node is taken from the availability stack and
linked to the new list.
On other end, the deleted node from the list is added to the availability stack.
FIRST
= 350 10 50 -1 8 35 44 50
350 700 300 50 450 400 2000
LINK(400) = NULL
If FIRST != NULL
then TEMP = FIRST = 350
while ( LINK(TEMP) != NULL)
TEMP = LINK ( TEMP)
LINK(TEMP) = NEW
Function: INSEND(X, First) Cont…
1. [Underflow?] 5. [Is the list empty?]
If AVAIL = NULL If FIRST = NULL
Then Write (“Availability Then Return (NEW)
Stack Underflow”)
Return(FIRST) 6. [Initialize search for a last node]
TEMP🡨 FIRST
2. [Obtain address of next free Node]
NEW 🡨 AVAIL 7. [Search for end of list]
Repeat while LINK (TEMP) ≠ NULL
3. [Remove free node from availability TEMP 🡨 LINK (TEMP)
Stack]
AVAIL 🡨 LINK(AVAIL) 8. [Set link field of last node to NEW]
LINK (TEMP) 🡨 NEW
4. [Initialize fields of new node]
INFO(NEW) 🡨 X 9. [Return first node pointer]
LINK (NEW) 🡨 NULL Return (FIRST)
Function: INSEND(50, FIRST)
4. [Initialize fields of new node] 7. [Search for end of list]
INFO(NEW) 🡨 X Repeat while LINK (SAVE) ≠ NULL
LINK (NEW) 🡨 NULL SAVE 🡨 LINK (SAVE)
5. [Is the list empty?] 8. [Set link field of last node to NEW]
If FIRST = NULL LINK (SAVE) 🡨 NEW
Then Return (NEW)
9. [Return first node pointer]
6. [Initialize search for a last node] Return (FIRST)
SAVE🡨 FIRST
SAVE
10 50 -1 8 35 44 50
NEW
FIRST
Function: INSORD(X, FIRST)
This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their INFO field.
This function returns address of FIRST node.
X is a new element to be inserted.
FIRST is a pointer to the first element of a Singly linked linear list.
Typical node contains INFO and LINK fields.
AVAIL is a pointer to the top element of the availability stack.
NEW is a temporary pointer variable. Predecessor
5 10 15 20 25 30
FIRST
22
NEW
Function: INSORD(X, FIRST)
1. [Underflow?] 6. [Does the new node precede all other
IF AVAIL = NULL node in the list?]
THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
Stack Underflow”) THEN LINK (NEW) 🡨 FIRST
Return(FIRST) Return (NEW)
2. [Obtain address of next free Node] 7. [Initialize temporary pointer]
NEW 🡨 AVAIL SAVE 🡨 FIRST
3. [Remove free node from availability 8. [Search for predecessor of new node]
Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL 🡨 LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of new node] SAVE 🡨 LINK (SAVE)
INFO(NEW) 🡨 X 9. [Set link field of NEW node and its
5. [Is the list empty?] predecessor]
IF FIRST = NULL LINK (NEW) 🡨 LINK (SAVE)
THEN LINK(NEW) 🡨 NULL LINK (SAVE) 🡨 NEW
Return (NEW) 10. [Return first node pointer]
Return (FIRST)
Function: INSORD(3, FIRST)
5 10 15 20 25 30
FIRST
3
NEW
6. [Does the new node precede all other node in the list?]
IF INFO(NEW) ≤ INFO (FIRST)
THEN LINK (NEW) 🡨 FIRST
Return (NEW)
Function: INSORD(22, FIRST)
7. [Initialize temporary pointer] 9. [Set link field of NEW node and its
SAVE 🡨 FIRST predecessor]
8. [Search for predecessor of new node] LINK (NEW) 🡨 LINK (SAVE)
Repeat while LINK (SAVE) ≠ NULL LINK (SAVE) 🡨 NEW
& INFO(NEW) ≥ INFO(LINK(SAVE)) 10. [Return first node pointer]
SAVE 🡨 LINK (SAVE) Return (FIRST)
SAVE
5 10 15 20 25 30
FIRST
22
NEW
Procedure: DELETE(X, FIRST)
This algorithm delete a node whose address is given by variable X.
FIRST is a pointer to the first element of a Singly linked linear list.
Typical node contains INFO and LINK fields.
SAVE & PRED are temporary pointer variable.
PRED SAVE
5 10 15 20 25 30
FIRST
Procedure: DELETE( X, FIRST)
1. [Is Empty list?] 5. [Move to next node]
IF FIRST = NULL SAVE 🡨 LINK(SAVE)
THEN write (‘Underflow’) 6. [End of the list?]
Return If SAVE ≠ X
2. [Initialize search for X] THEN write (‘Node not found’)
SAVE 🡨 FIRST Return
3. [Find X] 7. [Delete X]
Repeat thru step-5 If X = FIRST
while SAVE ≠ X and THEN FIRST 🡨 LINK(FIRST)
LINK (SAVE) ≠ NULL ELSE LINK (PRED) 🡨 LINK (X)
4. [Update predecessor marker] 8. [Free Deleted Node]
PRED 🡨 SAVE Free (X)
Procedure: DELETE(7541, FIRST)
2. [Initialize search for X] 6. [End of the list?]
SAVE 🡨 FIRST If SAVE ≠ X
3. [Find X] THEN Write (‘Node not found’)
Repeat thru step-5 Return
while SAVE ≠ X and 7. [Delete X]
LINK (SAVE) ≠ NULL If X = FIRST
4. [Update predecessor marker] THEN FIRST 🡨 LINK(FIRST)
PRED 🡨 SAVE ELSE LINK (PRED) 🡨 LINK (X)
5. [Move to next node] 8. [Free Deleted Node]
SAVE 🡨 LINK(SAVE) Free (X)
SAVE
PRED
5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST
Function: COUNT_NODES(FIRST)
This function counts number of nodes of the linked list and returns COUNT.
FIRST is a pointer to the first element of a Singly linked linear list.
Typical node contains INFO and LINK fields.
SAVE is a Temporary pointer variable.
5
NEW
Function: COPY (FIRST)
3. [Initialize Traversal] 6. [Copy Node]
SAVE 🡨 FIRST IF AVAIL = NULL
4. [Move the next node if not at the end THEN write (‘Underflow’)
if list] Return (0)
Repeat thru step 6 ELSE NEW 🡨 AVAIL
while LINK(SAVE) ≠ NULL AVAIL 🡨 LINK(AVAIL)
5. [Update predecessor and save pointer] FIELD(NEW)🡨INFO(SAVE)
PRED🡨NEW PTR(PRED)🡨NEW
SAVE🡨LINK(SAVE) 7. [Set link of last node & return]
PTR(NEW) 🡨 NULL
FIRST SAVE
Return(BEGIN)
5 10 15 30
BEGIN
PRED
5 10 15 30
NEW NEW NEW NEW
Circularly Linked Linear List
If we replace NULL pointer of the last node of Singly Linked Linear List with the address of its
first node, that list becomes circularly linked linear list or Circular List.
FIRST is the address of first node of Circular List
LAST is the address of the last node of Circular List
Advantages of Circular List
In circular list, every node is accessible from given node
It saves time when we have to go to the first node from the last node. It can be done in single step because
there is no need to traverse the in between nodes. But in double linked list, we will have to go through in
between nodes LAST
5 10 15 20 25 30
FIRST
Circularly Linked Linear List Cont…
Disadvantages of Circular List
It is not easy to reverse the linked list.
If proper care is not taken, then the problem of infinite loop can occur.
If we at a node and go back to the previous node, then we can not do it in single step. Instead we have to
complete the entire circle by going through the in between nodes and then we will reach the required node.
50
NEW 5 10 1 -5
FIRST
Procedure: CIR_INS_LAST(X,FIRST,LAST)
This procedure inserts a new node at the last position of Circular linked list.
X is a new element to be inserted.
FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains INFO and LINK fields.
NEW is a temporary pointer variable.
Procedure: CIR_INS_LAST( X,FIRST,LAST)
1. [Creates a new empty node] IF FIRST = NULL
NEW NODE THEN LINK (NEW) 🡨 NEW
2. [Initialize fields of new node FIRST 🡨 LAST 🡨 NEW
and its link] ELSE LINK (NEW) 🡨 FIRST
INFO (NEW) 🡨 X LINK (LAST) 🡨 NEW
LAST 🡨 NEW
Return
LAST
NEW
FIRST LAST
50
LAST
50
NEW
5 10 1 -5
FIRST
Procedure: CIR_INS_ORD(X,FIRST,LAST)
This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their INFO field.
X is a new element to be inserted.
FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains INFO and LINK fields.
NEW is a temporary pointer variable.
Procedure: CIR_INS_ORD(X,FIRST,LAST)
1. [Create New Empty Node] 5. [Initialize Temporary Pointer]
NEW NODE SAVE 🡨 FIRST
2. [Copy information content into new 6. [Search for Predecessor of new node]
node] Repeat while SAVE ≠ LAST &
INFO(NEW) 🡨 X INFO(NEW) ≥ INFO(LINK(SAVE))
3. [Is Linked List Empty?] SAVE🡨LINK(SAVE)
IF FIRST = NULL 7. [Set link field of NEW node and its
THEN LINK(NEW) 🡨 NEW Predecessor]
FIRST 🡨 LAST 🡨 NEW LINK(NEW) 🡨 LINK(SAVE)
Return LINK(SAVE) 🡨 NEW
4. [Does new node precedes all other IF SAVE = LAST
nodes in List?] THEN LAST 🡨 NEW
IF INFO(NEW)≤ INFO(FIRST) 8. [Finished]
THEN LINK(NEW) 🡨 FIRST Return
LINK(LAST) 🡨 NEW
FIRST 🡨 NEW
Return
Procedure: CIR_INS_ORD(3,FIRST,LAST)
1. [Create New Empty Node] 4. [Does new node precedes all other
NEW NODE nodes in List?]
2. [Copy information content into new IF INFO(NEW)≤ INFO(FIRST)
node] THEN LINK(NEW) 🡨 FIRST
INFO(NEW) 🡨 X LINK(LAST) 🡨 NEW
3. [Is Linked List Empty?] FIRST 🡨 NEW
IF FIRST = NULL Return
THEN LINK(NEW) 🡨 NEW
FIRST 🡨 LAST 🡨 NEW FIRST
Return NEW
FIRST LAST 3
LAST
3
5 10 15 20
NEW
FIRST
Procedure: CIR_INS_ORD(18,FIRST,LAST)
5. [Initialize Temporary Pointer] 7. [Set link field of NEW node and its
SAVE 🡨 FIRST Predecessor]
6. [Search for Predecessor of new node] LINK(NEW) 🡨 LINK(SAVE)
Repeat while SAVE ≠ LAST & LINK(SAVE) 🡨 NEW
INFO(NEW) ≥ INFO(LINK(SAVE)) IF SAVE = LAST
SAVE🡨LINK(SAVE) THEN LAST 🡨 NEW
8. [Finished]
NEW Return
18
LAST
SAVE
5 10 15 20
FIRST
Procedure: CIR_DELETE(X,FIRST,LAST)
This algorithm delete a node whose address is given by variable X.
FIRST & LAST are pointers to the First & Last elements of a Circular linked list, respectively.
Typical node contains INFO and LINK fields.
SAVE & PRED are temporary pointer variable.
Procedure: CIR_DELETE(X,FIRST,LAST)
1. [Is Empty List?] 6. [End of Linked List?]
IF FIRST = NULL IF SAVE ≠ X
THEN write(‘Linked List is THEN write(‘Node not found’)
Empty’) Return
Return 7. [Delete X]
2. [Initialize Search for X] IF X = FIRST
SAVE 🡨 FIRST THEN FIRST🡨LINK(FIRST)
3. [Find X] LINK(LAST)🡨FIRST
Repeat thru step 5 ELSE LINK(PRED)🡨LINK(X)
while SAVE≠X & SAVE≠LAST IF X = LAST
4. [Update predecessor marker] THEN LAST 🡨 PRED
PRED 🡨 SAVE 8. [Free Deleted Node]
5. [Move to next node] Free (X)
SAVE 🡨 LINK(SAVE)
Procedure: CIR_DELETE(7541,FIRST,LAST)
1. [Is Empty List?] 6. [End of Linked List?]
IF FIRST = NULL IF SAVE ≠ X
THEN write(‘Linked List is Empty’) THEN write(‘Node not found’)
Return Return
2. [Initialize Search for X] 7. [Delete X]
SAVE 🡨 FIRST IF X = FIRST
3. [Find X] THEN FIRST🡨LINK(FIRST)
Repeat thru step5 while SAVE≠X & SAVE≠LAST LINK(LAST)🡨FIRST
4. [Update predecessor marker] ELSE LINK(PRED)🡨LINK(X)
PRED 🡨 SAVE IF X = LAST
5. [Move to next node] THEN LAST 🡨 PRED
SAVE 🡨 LINK(SAVE) 8. [Free Deleted Node]
Free (X)
SAVE
PRED
5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST LAST
Circularly Linked List with Header Node
We can have special node, often referred to as Head node of Circular Linked List.
Head node does not have any value.
Head node is always pointing to the first node if any of the linked list.
One advantage of this technique is Linked list is never be empty.
Pointer variable HEAD contains the address of head node.
HEAD
10 15 20 25 30
HEAD
Empty List
LINK(HEAD) 🡨 HEAD
Procedure: CIR_HEAD_INS_FIRST(X,FIRST,LAST)
This procedure inserts a new node at the first position of Circular linked list with Head node.
X is a new element to be inserted.
FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains INFO and LINK fields.
HEAD is pointer variable pointing to Head node of Linked List.
NEW is a temporary pointer variable.
Procedure: CIR_HEAD_INS_FIRST(X,FIRST,LAST)
1. [Create New Empty Node]
NEW NODE
2. [Initialize fields of new node and its link to the list]
INFO(NEW) 🡨 X
LINK(NEW) 🡨 LINK(HEAD)
LINK(HEAD) 🡨 NEW
NEW
50
HEAD
10 15 20 25 30
Procedure: CIR_HEAD_INS_LAST(X,FIRST,LAST)
This procedure inserts a new node at the last position of Circular linked list with Head node.
X is a new element to be inserted.
FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains INFO and LINK fields.
HEAD is pointer variable pointing to Head node of Linked List.
NEW is a temporary pointer variable.
Procedure: CIR_HEAD_INS_LAST(X,FIRST,LAST)
1. [Create New Empty Node]
NEW NODE
2. [Initialize fields of new node and its link to the list]
INFO(NEW) 🡨 X
LINK(NEW) 🡨 HEAD
LINK(LAST) 🡨 NEW
LAST 🡨 NEW
NEW
50
HEAD
10 15 20 25
FIRST LAST
Procedure: CIR_HEAD_INS_AFTER-P (X,FIRST,LAST)
This procedure inserts a new node after a node whose address is given by P of Circular linked
list with Head node.
X is a new element to be inserted.
FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains INFO and LINK fields.
HEAD is pointer variable pointing to Head node of Linked List.
NEW is a temporary pointer variable.
Procedure: CIR_HEAD_INS_AFTER-P (X,FIRST,LAST)
1. [Create New Empty Node]
NEW NODE
2. [Initialize fields of new node and its link to the list]
INFO(NEW) 🡨 X
LINK(NEW) 🡨 LINK(P)
LINK(P) 🡨 NEW
IF P = LAST
THEN LAST 🡨 NEW
NEW
50
HEAD P
10 15 20 25
FIRST LAST
Doubly Linked Linear List
In certain Applications, it is very desirable that a list be traversed in either forward or reverse
direction.
This property implies that each node must contain two link fields instead of usual one.
The links are used to denote Predecessor and Successor of node.
The link denoting its predecessor is called Left Link.
The link denoting its successor is called Right Link.
A list containing this type of node is called doubly linked list or two way chain.
Doubly Linked Linear List
Typical node of doubly linked linear list contains INFO, LPTR RPTR Fields
LPTR is pointer variable pointing to Predecessor of a node
RPTR is pointer variable pointing to Successor of a node
Left most node of doubly linked linear list is called L, LPTR of node L is always NULL
Right most node of doubly linked linear list is called R, RPTR of node R is always NULL
Typical node of
Doubly Linked List L Doubly linked linear list R
Insert node in Doubly Linked List
Insertion in the middle of Doubly Linked Linear List
Before Insertion M
L R
LPTR(NEW) 🡨 LPTR(M)
NEW RPTR(NEW) 🡨 M
LPTR(M) 🡨 NEW
RPTR(LPTR(NEW)) 🡨 NEW
After Insertion
M
L R
NEW
Insert node in Doubly Linked List
Left most insertion in Doubly Linked Linear List
Before Insertion
M
L R
LPTR(NEW) 🡨 NULL
RPTR(NEW) 🡨 M
NEW LPTR(M) 🡨 NEW
L 🡨 NEW
M After Insertion
L
L R
NEW
Procedure: DOU_INS (L,R,M,X)
This algorithm inserts a new node in doubly linked linear list.
The insertion is to be performed to the left of a specific node with its address given by the
pointer variable M.
Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
LPTR is pointer variable pointing to Predecessor of a node.
RPTR is pointer variable pointing to Successor of a node.
L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
NEW is the address of New Node.
X is value to be inserted.
Procedure: DOU_INS (L,R,M,X)
1. [Create New Empty Node] 4. [Is left most insertion ?]
NEW NODE IF M = L
2. [Copy information field] THEN LPTR(NEW) 🡨 NULL
INFO(NEW) 🡨 X RPTR(NEW) 🡨 M
3. [Insert into an empty list] LPTR(M)🡨 NEW
IF R = NULL L 🡨 NEW
THEN LPTR(NEW) 🡨 NULL Return
RPTR(NEW) 🡨 NULL 5. [Insert in middle]
L 🡨 R 🡨 NEW LPTR(NEW) 🡨 LPTR(M)
Return RPTR(NEW) 🡨 M
LPTR(M) 🡨 NEW
RPTR(LPTR(NEW)) 🡨 NEW
Return
PROCEDURE: DOU _DEL (L, R, OLD)
This algorithm deletes the node whose address is contained in the variable OLD.
Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
LPTR is pointer variable pointing to Predecessor of a node.
RPTR is pointer variable pointing to Successor of a node.
L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
Delete from Doubly Linked List
10 L 🡨 R 🡨 NULL
OLD
L R
L L R R
Thank
You