0% found this document useful (0 votes)
14 views31 pages

LinkedLists MCH

Uploaded by

m.biswas2014.mb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

LinkedLists MCH

Uploaded by

m.biswas2014.mb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

LINKED LISTS

Dr. Maumita Chakraborty


University of Engineering and Management Kolkata
INTRODUCTION
 A linear collection of data elements called nodes.
 Acts as building block to implement other data structures like stacks,
queues etc.
 A sequence of nodes in which each node contain one or more data
fields and a pointer to the next node.
HEAD

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

Algorithm for traversing a linked list

Step 1: [INITIALIZE] SET PTR = HEAD


Step 2: Repeat Steps 3 and 4 while PTR ≠ NULL
Step 3: Write PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Insertion in Singly Linked List
HEAD
1 7 3 4 2 6 5 X

HEAD , New_Node
9 1 7 3 4 2 6 5 X

Algorithm to insert a new node in the beginning of the linked list

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 10
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET New_Node->Next = HEAD
Step 9: SET HEAD = New_Node
Step 10: EXIT
1 7 3 4 2 6 5 X

HEAD, PTR

1 7 3 4 2 6 5 9 X

HEAD PTR New_Node

Algorithm to insert a new node at the end of the linked list

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 13
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET New_Node->Next = NULL
Step 9: SET PTR = HEAD
Step 10: Repeat Step 11 while PTR->NEXT ≠ NULL
Step 11: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 12: SET PTR->NEXT = New_Node
Step 13: EXIT
Algorithm to insert a new node after a node that has value NUM

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 13
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET PTR = HEAD
Step 9: Repeat Step 10 while PTR->DATA ≠ NUM
Step 10: PTR = PTR->NEXT
[END OF LOOP]
Step 11: SET New_Node->NEXT = PTR->NEXT
Step 12: PTR->NEXT = New_Node
Step 13: EXIT

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 New_Node


Deletion in Singly Linked List
1 7 3 4 2 6 5 X

HEAD, PTR

7 3 4 2 6 5 X

HEAD

Algorithm to delete the first node from the linked list

Step 1: IF HEAD = NULL, then


Step 2: Write UNDERFLOW
Step 3: Go to Step 9
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: SET HEAD = HEAD->NEXT
Step 7: PTR->NEXT=NULL
Step 8: FREE PTR
Step 9: EXIT
1 7 3 4 2 6 5 X

HEAD, PREPTR, PTR

1 7 3 4 2 6 X 5 X

HEAD PREPTR PTR

Algorithm to delete the last node of the 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:
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

HEAD, PREPTR, PTR

1 7 3 4 2 6 5 X

HEAD PREPTR PTR

1 7 3 4 2 6 5 X

HEAD PREPTR PTR

1 7 3 4 6 5 X

HEAD PREPTR

Algorithm to delete a given node from the linked list


Step 1: IF HEAD = NULL, then
Step 2: Write UNDERFLOW
Step 3: Go to Step 14
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
Other Operations in Singly Linked Lists
 Concatenation of 2 Singly Linked Lists
 Hints:
 ptr:=head1
 while (ptr->next ≠ NULL)
 ptr:=ptr->next
 ptr->next:=head2

 Reverse Printing a Singly LL


 Hints:
 ptr:=NULL
 while(ptr ≠ head)
 temp:=head
 while(temp->next ≠ ptr)
 temp:=temp->next
 print(temp->data)
 ptr:=temp
Other Operations in Singly Linked Lists
 Reversing a Singly LL
 Hints:
 node *curr:=head, *res:=head->next, *prev:=NULL
 while(curr ≠ NULL)
 curr->next:=prev
 prev:=curr
 curr:=res
 If(res ≠ NULL)
 res:=res->next
 head:=curr

 Sorting a Singly LL (swapping of data)


 Sorting a Singly LL (swapping of nodes)
 Practice any one sorting algorithm on Singly LL
Other Operations in Singly Linked Lists
 Searching in a Singly LL
 Hints:
 pos:=0
 ptr:=head
 while(ptr->data ≠ elmnt)
 ptr:=ptr->next
 pos++
 if(ptr ≠ NULL)
 print(pos)
 Else
 print(“Element not found”)
Doubly and Circular Linked List
Doubly Linked List
• Contains a pointer to the next as well as previous node in the
sequence.
• Consists of three parts: data, a pointer to the next node, and a
pointer to the previous node
HEAD

X 1 1 2 3 4 X

In C language, the structure of a doubly linked list is given as:


struct node
{ struct node *prev;
int data;
struct node *next;
};
• prev field of the first node and next field of the last node contain
NULL.
• prev field used to store address of the preceding node which enables
the list to be traversed in backward direction as well.
Algorithm to insert a new node in the beginning of the doubly
linked list

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 12
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET New_Node->PREV = NULL
Step 9: SET New_Node->Next = HEAD
Step 10: SET HEAD->PREV = New_Node
Step 11: SET HEAD = New_Node
Step 12: EXIT

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

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 14
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET New_Node->Next = NULL
Step 9: SET PTR = HEAD
Step 10: Repeat Step 11 while PTR->NEXT ≠ NULL
Step 11: SET PTR = PTR->NEXT
[END OF LOOP]
Step 12: SET PTR->NEXT = New_Node
Step 13: New_Node->PREV = PTR
Step 14: EXIT

X 1 7 3 4 2 X

HEAD, PTR

X 1 7 3 4 2 9 X

HEAD PTR New_Node


Algorithm to insert a new node after a node that has value NUM
Step 1: IF AVAIL = NULL, then
Step 2: Write OVERFLOW
Step 3: Go to Step 15
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET PTR = HEAD
Step 9: Repeat Step 10 while PTR->DATA ≠ NUM
Step 10: SET PTR = PTR->NEXT
[END OF LOOP]
Step 11: New_Node->NEXT = PTR->NEXT
Step 12: SET New_Node->PREV = PTR
Step 13: SET PTR->NEXT->PREV = New_Node
Step 14: SET PTR->NEXT = New_Node
Step 15: EXIT

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

Step 1: IF HEAD = NULL, then


Step 2: Write UNDERFLOW
Step 3: Go to Step 10
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: SET HEAD = HEAD->NEXT
Step 7: SET HEAD->PREV = NULL
Step 8: SET PTR->PREV = PTR->NEXT = NULL
Step 9: FREE PTR
Step 10: EXIT

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

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 while PTR->NEXT ≠ NULL
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: SET PTR->PREV->NEXT = NULL
Step 9: SET PTR->PREV = PTR->NEXT = NULL
Step 10: FREE PTR
Step 11: EXIT

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

Step 1: IF HEAD = NULL, then


Step 2: Write UNDERFLOW
Step 3: Go to Step 12
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: Repeat Step 7 while PTR->DATA ≠ NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: SET PTR->NEXT->PREV = PTR->PREV
Step 9: SET PTR->PREV->NEXT = PTR->NEXT
Step 10: SET PTR->NEXT = PTR->PREV = NULL
Step 11: FREE PTR
Step 12: EXIT

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.

 It can be a circular singly linked list as well as circular doubly


linked list.

HEAD

1 2 3 4 5 6 7
Algorithm to insert a new node in the beginning of circular linked list

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 14
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET PTR = HEAD
Step 9: Repeat Step 10 while PTR->NEXT ≠ HEAD
Step 10: PTR = PTR->NEXT
Step 11: SET New_Node->Next = HEAD
Step 12: SET PTR->NEXT = New_Node
Step 13: SET HEAD = New_Node
Step 14: EXIT

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

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 13
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET New_Node->Next = HEAD
Step 9: SET PTR = HEAD
Step 10: Repeat Step 11 while PTR->NEXT ≠ HEAD
Step 11: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 12: SET PTR ->NEXT = New_Node
Step 13: EXIT

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

Step 1: IF AVAIL = NULL, then


Step 2: Write OVERFLOW
Step 3: Go to Step 14
Step 4: [END OF IF]
Step 5: SET New_Node = AVAIL
Step 6: SET AVAIL = AVAIL->NEXT
Step 7: SET New_Node->DATA = VAL
Step 8: SET PTR = HEAD
Step 9: SET PREPTR = PTR
Step 10: Repeat Step 11 while PTR->DATA ≠ NUM
Step 11: SET PTR = PTR->NEXT
[END OF LOOP]
Step 12: New_Node->NEXT = PTR->NEXT
Step 13: SET PTR->NEXT = New_Node
Step 14: EXIT

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

Step 1: IF HEAD = NULL, then


Step 2: Write UNDERFLOW
Step 3: Go to Step 12
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: Repeat Step 7 while PTR->NEXT ≠ HEAD
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: SET PTR->NEXT = HEAD->NEXT
Step 9: SET HEAD->NEXT = NULL
Step 10: FREE HEAD
Step 11: SET HEAD = PTR->NEXT
Step 12: EXIT

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

Step 1: IF HEAD = NULL, then


Step 2: Write UNDERFLOW
Step 3: Go to Step 12
Step 4: [END OF IF]
Step 5: SET PTR = HEAD
Step 6: Repeat Steps 7 and 8 while PTR->NEXT ≠ HEAD
Step 7: SET PREPTR = PTR
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PREPTR->NEXT = HEAD
Step 10: SET PTR->NEXT = NULL
Step 11: FREE PTR
Step 12: 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 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

 Above linked list represented by 2 arrays:


 1) Data Array (holds the data parts of each node)

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

 Singly Linked List


 Polynomial Representation and Operations (Addition,
Subtraction and Multiplication of 2 polynomials using LL)
 Double Linked List
 Sparse Matrix Representation and Operations
 Circular Linked List
 Josephus Problem Implementation

You might also like