SlideShare a Scribd company logo
Double & Circular Linked Lists
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Insertion in Linked List with following criteria
▫ Start of the list
▫ End of the list
▫ Middle of the list
▫ Anywhere in the list
• Deletion of a node from
▫ The Tail of the List
▫ The Head of the List
▫ A desired location in the list
▫ Delete node with a particular value
Objectives Overview
• Introduction to Double Linked List
• Insertions and Deletions in Doubly Linked List
• Introduction to Circular Linked List
• Insertion and Deletion in Circular Linked List
Doubly Linked List
• In a doubly linked list, each node contains a data part
and two addresses, one for the previous node and
one for the next node.
Doubly Linked List - Representation
Inserting into a Doubly Linked List
Inserting into a Doubly Linked List
A Node can be added in four ways:
1) At the front of the DLL
2) After a given node.
3) At the end of the DLL
4) Before a given node.
Insertion at the Front - Algorithm
Steps:
1. Allocate node
2. Put in the data
3. Make next of new node as head and
previous as NULL
4. Change previous of head node to new
node
5. Move the head to point to the new
node
Insertion at the Front - Representation
Insertion at the Front - Implementation
• void push(struct Node** head_ref, int new_data) {
• struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
• new_node->data = new_data;
• new_node->next = (*head_ref);
• new_node->prev = NULL;
• if ((*head_ref) != NULL)
▫ (*head_ref)->prev = new_node;
• (*head_ref) = new_node;
• }
Insertion after a given Node - Algorithm
Steps:
1. Check if the given previous node is NULL
2. Allocate new node
3. Put in the data
4. Make next of new node as next of previous
node
5. Make the next of previous node as new
node
6. Make previous node as previous of new
node
7. Change previous of new node's next node
Insertion after a given Node - Representation
Insertion after a given Node - Implementation
• void insertAfter(struct Node* prev_node, int new_data)
• {
• if (prev_node == NULL) {
• printf("the given previous node cannot be NULL");
• return;
• }
• struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
• new_node->data = new_data;
• new_node->next = prev_node->next;
• prev_node->next = new_node;
• new_node->prev = prev_node;
• if (new_node->next != NULL)
• new_node->next->prev = new_node;
• }
Insertion at the End - Algorithm
Steps:
1. Allocate node
2. Put in the data
3. This new node is going to be the last node, so
make next of it as NULL
4. If the Linked List is empty, then make the new
node as head
5. Else traverse till the last node
6. Change the next of last node
7. Make last node as previous of new node
Insertion at the End - Representation
Insertion at the End - Implementation
• void append(struct Node** head_ref, int new_data)
• {
• struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
•
• struct Node* last = *head_ref; /* used in step 5*/
• new_node->data = new_data;
• new_node->next = NULL;
•
• if (*head_ref == NULL) {
• new_node->prev = NULL;
• *head_ref = new_node;
• return;
• }
• while (last->next != NULL)
• last = last->next;
• last->next = new_node;
• new_node->prev = last;
•
• return;
• }
Insertion before a given Node - Algorithm
Steps:
1. Check if the given next node is NULL
2. Allocate new node
3. Put in the data
4. Make previous of new node as previous of next node
5. Make the previous of next node as new node
6. Make next node as next of new node
7. Change next of new node's previous node
Insertion before a given Node - Representation
Insertion before a given Node - Implementation
• void insertBefore(struct Node* next_node, int new_data)
• {
• if (next_node == NULL) {
• printf("the given next node cannot be NULL");
• return;
• }
• struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
• new_node->data = new_data;
• new_node->prev = next_node->prev;
• next_node->prev = new_node;
• new_node->next = next_node;
• if (new_node->prev != NULL)
• new_node->prev->next = new_node;
• }
Deletion in Double Linked List
Algorithm
Let the node to be deleted is del.
1) If node to be deleted is head node, then change the
head pointer to next current head.
2) Set next of previous to del, if previous to del exists.
3) Set prev of next to del, if next to del exists.
Original Doubly Linked List
After deletion of head node
After deletion of middle node
After deletion of last node
Advantages over Singly Linked List
• 1) A DLL can be traversed in both forward and backward
direction.
2) The delete operation in DLL is more efficient if pointer to
the node to be deleted is given.
3) We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous
node is needed. To get this previous node, sometimes the list
is traversed. In DLL, we can get the previous node using
previous pointer.
Disadvantages over Singly Linked List
• 1) Every node of DLL Require extra space for an previous
pointer. It is possible to implement DLL with single pointer
though.
2) All operations require an extra pointer previous to be
maintained. For example, in insertion, we need to modify
previous pointers together with next pointers. For example in
following functions for insertions at different positions, we
need 1 or 2 extra steps to set previous pointer.
Circular Linked List
Circular Linked List
• In circular linked list the last node of the list holds
the address of the first node hence forming a circular
chain.
Circular Linked List - Representation
Inserting into a Circular Linked List
Insertion
A Node can be inserted in four ways:
1. Insertion in an empty list
2. Insertion at the beginning of the
list
3. Insertion at the end of the list
4. Insertion in between the nodes
Insertion in an Empty List - Algorithm
• Initially when the list is empty, last pointer will be
NULL.
• Insert a node T,
• After insertion, T is the last node so pointer last
points to node T.
• Node T is first and last node, so T is pointing to itself.
Insertion in an Empty List - Representation
Implementation
struct Node *addToEmpty(struct Node *last, int data)
{
// This function is only for empty list
if (last != NULL)
return last;
// Creating a node dynamically.
struct Node *last =
(struct Node*)malloc(sizeof(struct Node));
// Assigning the data.
last -> data = data;
// Note : list was empty. We link single node
// to itself.
last -> next = last;
return last;
}
Insertion at the Beginning - Algorithm
To Insert a node at the beginning of the list, follow
these steps:
1. Create a node, say T.
2. Make T -> next = last -> next.
3. last -> next = T.
Insertion at the Beginning - Representation
Insertion at the Beginning - Representation
After insertion
Implementation
struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
// Adjusting the links.
temp -> next = last -> next;
last -> next = temp;
return last;
}
Insertion at the End - Algorithm
To Insert a node at the end of the list, follow these
steps:
1. Create a node, say T.
2. Make T -> next = last -> next;
3. last -> next = T.
4. last = T.
Insertion at the End - Representation
Insertion at the End - Representation
After insertion
Implementation
struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
Insertion in between the Nodes - Algorithm
To Insert a node in between the Nodes, follow these
steps:
1. Create a node, say T.
2. Search the node after which T need to be insert,
say that node be P.
3. Make T -> next = P -> next;
4. P -> next = T.
Insertion in between the Nodes -
Representation
Insertion in between the Nodes - Representation
After searching and insertion
Implementation
struct Node *addAfter(struct Node *last, int data, int item)
{
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == item)
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while (p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}
Deletion in Circular Linked List
Deletion in Circular Linked List
There are three situation for Deleting element in list.
1. Deletion at beginning.
2. Deletion at the middle.
3. Deletion at the end.
Deletion - Algorithm
Steps:
• Create a circular linked list and assign reference of first node to head.
• Input key to delete from user. Store it in some variable say key. Say key to
delete is 20.
• To keep track of previous node and node to delete, declare two variables
of node type. Say cur = head and prev. Make sure previous points to last
node.
• If current node contains key, Then you got node to delete.
• Before deleting a node, you must first adjust previous node link.
• Adjust head node if needed.
• Delete the node.
• Update current node, i.e. assign cur = previous->next if previous != NULL.
Otherwise assign NULL.
• If current node does not contain key to delete, then simply update
previous and current node.
• Repeat step 3-4 till last node.
Representation
Deletion at the middle of the Circular linked list
Representation
After Deletion
Representation
Deletion at the middle of the Circular linked list
Representation
After Deletion
Representation
Deletion at the End of the Circular linked list
Representation
After Deletion
Implementation
• void delete_first(struct link *node) {
•
• node=start->next;
•
• ptr=start;
•
• if(i==0) {
•
• printf("n List is empty");
• exit(0);
• }
•
• ptr->next=node->next;
• free(node);
•
• i--;
• }
Summary
• Introduction to Double Linked List
• Insertions and Deletions in Doubly Linked List
• Introduction to Circular Linked List
• Insertion and Deletion in Circular Linked List
References
• https://www.geeksforgeeks.org/doubly-linked-list/
• http://scanftree.com/Data_Structure/Deletion-in-
circular-linked-list
• https://www.tutorialspoint.com/data_structures_alg
orithms/circular_linked_list_algorithm.htm
• https://codeforwin.org/2018/06/c-program-to-
delete-element-from-circular-linked-list.html
• https://www.cse.unr.edu/~bebis/CS308/PowerPoint/
LinkedDoublyLists.ppt

More Related Content

What's hot

Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
RacksaviR
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
sajinis3
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
Akila Krishnamoorthy
 
Trees
TreesTrees
Singly link list
Singly link listSingly link list
Singly link list
Rojin Khadka
 
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
Usha Mahalingam
 
Linked list
Linked listLinked list
Linked list
RahulGandhi110
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
Keval Bhogayata
 

What's hot (20)

Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
single linked list
single linked listsingle linked list
single linked list
 
Trees
TreesTrees
Trees
 
Singly link list
Singly link listSingly link list
Singly link list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked list Linked list
Linked list
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Linked lists
Linked listsLinked lists
Linked lists
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 

Similar to Doubly & Circular Linked Lists

Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
ChrisSosaJacob
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
kiran Patel
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
SwarChaudhary
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
NirmalPandey23
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
PoonamPatil120
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
AdithaBuwaneka
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
KasthuriKAssistantPr
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
Kaynattariq1
 
LinkedDoublyLists.ppt
LinkedDoublyLists.pptLinkedDoublyLists.ppt
LinkedDoublyLists.ppt
veenatanmaipatlolla
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
jack881
 

Similar to Doubly & Circular Linked Lists (20)

Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Linked list
Linked listLinked list
Linked list
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Team 10
Team 10Team 10
Team 10
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
 
Data structures
Data structuresData structures
Data structures
 
LinkedDoublyLists.ppt
LinkedDoublyLists.pptLinkedDoublyLists.ppt
LinkedDoublyLists.ppt
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 

More from Afaq Mansoor Khan

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
Afaq Mansoor Khan
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
Afaq Mansoor Khan
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
Afaq Mansoor Khan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
Afaq Mansoor Khan
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
Afaq Mansoor Khan
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
Afaq Mansoor Khan
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
Afaq Mansoor Khan
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
Afaq Mansoor Khan
 
Quick sort
Quick sortQuick sort
Quick sort
Afaq Mansoor Khan
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
Afaq Mansoor Khan
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Afaq Mansoor Khan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
Afaq Mansoor Khan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Afaq Mansoor Khan
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 

More from Afaq Mansoor Khan (20)

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
 
Quick sort
Quick sortQuick sort
Quick sort
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 

Recently uploaded

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 

Recently uploaded (20)

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 

Doubly & Circular Linked Lists

  • 1. Double & Circular Linked Lists Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Insertion in Linked List with following criteria ▫ Start of the list ▫ End of the list ▫ Middle of the list ▫ Anywhere in the list • Deletion of a node from ▫ The Tail of the List ▫ The Head of the List ▫ A desired location in the list ▫ Delete node with a particular value
  • 3. Objectives Overview • Introduction to Double Linked List • Insertions and Deletions in Doubly Linked List • Introduction to Circular Linked List • Insertion and Deletion in Circular Linked List
  • 4. Doubly Linked List • In a doubly linked list, each node contains a data part and two addresses, one for the previous node and one for the next node.
  • 5. Doubly Linked List - Representation
  • 6. Inserting into a Doubly Linked List
  • 7. Inserting into a Doubly Linked List A Node can be added in four ways: 1) At the front of the DLL 2) After a given node. 3) At the end of the DLL 4) Before a given node.
  • 8. Insertion at the Front - Algorithm Steps: 1. Allocate node 2. Put in the data 3. Make next of new node as head and previous as NULL 4. Change previous of head node to new node 5. Move the head to point to the new node
  • 9. Insertion at the Front - Representation
  • 10. Insertion at the Front - Implementation • void push(struct Node** head_ref, int new_data) { • struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); • new_node->data = new_data; • new_node->next = (*head_ref); • new_node->prev = NULL; • if ((*head_ref) != NULL) ▫ (*head_ref)->prev = new_node; • (*head_ref) = new_node; • }
  • 11. Insertion after a given Node - Algorithm Steps: 1. Check if the given previous node is NULL 2. Allocate new node 3. Put in the data 4. Make next of new node as next of previous node 5. Make the next of previous node as new node 6. Make previous node as previous of new node 7. Change previous of new node's next node
  • 12. Insertion after a given Node - Representation
  • 13. Insertion after a given Node - Implementation • void insertAfter(struct Node* prev_node, int new_data) • { • if (prev_node == NULL) { • printf("the given previous node cannot be NULL"); • return; • } • struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); • new_node->data = new_data; • new_node->next = prev_node->next; • prev_node->next = new_node; • new_node->prev = prev_node; • if (new_node->next != NULL) • new_node->next->prev = new_node; • }
  • 14. Insertion at the End - Algorithm Steps: 1. Allocate node 2. Put in the data 3. This new node is going to be the last node, so make next of it as NULL 4. If the Linked List is empty, then make the new node as head 5. Else traverse till the last node 6. Change the next of last node 7. Make last node as previous of new node
  • 15. Insertion at the End - Representation
  • 16. Insertion at the End - Implementation • void append(struct Node** head_ref, int new_data) • { • struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); • • struct Node* last = *head_ref; /* used in step 5*/ • new_node->data = new_data; • new_node->next = NULL; • • if (*head_ref == NULL) { • new_node->prev = NULL; • *head_ref = new_node; • return; • } • while (last->next != NULL) • last = last->next; • last->next = new_node; • new_node->prev = last; • • return; • }
  • 17. Insertion before a given Node - Algorithm Steps: 1. Check if the given next node is NULL 2. Allocate new node 3. Put in the data 4. Make previous of new node as previous of next node 5. Make the previous of next node as new node 6. Make next node as next of new node 7. Change next of new node's previous node
  • 18. Insertion before a given Node - Representation
  • 19. Insertion before a given Node - Implementation • void insertBefore(struct Node* next_node, int new_data) • { • if (next_node == NULL) { • printf("the given next node cannot be NULL"); • return; • } • struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); • new_node->data = new_data; • new_node->prev = next_node->prev; • next_node->prev = new_node; • new_node->next = next_node; • if (new_node->prev != NULL) • new_node->prev->next = new_node; • }
  • 20. Deletion in Double Linked List
  • 21. Algorithm Let the node to be deleted is del. 1) If node to be deleted is head node, then change the head pointer to next current head. 2) Set next of previous to del, if previous to del exists. 3) Set prev of next to del, if next to del exists.
  • 23. After deletion of head node
  • 24. After deletion of middle node
  • 25. After deletion of last node
  • 26. Advantages over Singly Linked List • 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. 3) We can quickly insert a new node before a given node. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.
  • 27. Disadvantages over Singly Linked List • 1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though. 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.
  • 29. Circular Linked List • In circular linked list the last node of the list holds the address of the first node hence forming a circular chain.
  • 30. Circular Linked List - Representation
  • 31. Inserting into a Circular Linked List
  • 32. Insertion A Node can be inserted in four ways: 1. Insertion in an empty list 2. Insertion at the beginning of the list 3. Insertion at the end of the list 4. Insertion in between the nodes
  • 33. Insertion in an Empty List - Algorithm • Initially when the list is empty, last pointer will be NULL. • Insert a node T, • After insertion, T is the last node so pointer last points to node T. • Node T is first and last node, so T is pointing to itself.
  • 34. Insertion in an Empty List - Representation
  • 35. Implementation struct Node *addToEmpty(struct Node *last, int data) { // This function is only for empty list if (last != NULL) return last; // Creating a node dynamically. struct Node *last = (struct Node*)malloc(sizeof(struct Node)); // Assigning the data. last -> data = data; // Note : list was empty. We link single node // to itself. last -> next = last; return last; }
  • 36. Insertion at the Beginning - Algorithm To Insert a node at the beginning of the list, follow these steps: 1. Create a node, say T. 2. Make T -> next = last -> next. 3. last -> next = T.
  • 37. Insertion at the Beginning - Representation
  • 38. Insertion at the Beginning - Representation After insertion
  • 39. Implementation struct Node *addBegin(struct Node *last, int data) { if (last == NULL) return addToEmpty(last, data); struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); temp -> data = data; // Adjusting the links. temp -> next = last -> next; last -> next = temp; return last; }
  • 40. Insertion at the End - Algorithm To Insert a node at the end of the list, follow these steps: 1. Create a node, say T. 2. Make T -> next = last -> next; 3. last -> next = T. 4. last = T.
  • 41. Insertion at the End - Representation
  • 42. Insertion at the End - Representation After insertion
  • 43. Implementation struct Node *addEnd(struct Node *last, int data) { if (last == NULL) return addToEmpty(last, data); struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); temp -> data = data; temp -> next = last -> next; last -> next = temp; last = temp; return last; }
  • 44. Insertion in between the Nodes - Algorithm To Insert a node in between the Nodes, follow these steps: 1. Create a node, say T. 2. Search the node after which T need to be insert, say that node be P. 3. Make T -> next = P -> next; 4. P -> next = T.
  • 45. Insertion in between the Nodes - Representation
  • 46. Insertion in between the Nodes - Representation After searching and insertion
  • 47. Implementation struct Node *addAfter(struct Node *last, int data, int item) { if (last == NULL) return NULL; struct Node *temp, *p; p = last -> next; do { if (p ->data == item) { temp = (struct Node *)malloc(sizeof(struct Node)); temp -> data = data; temp -> next = p -> next; p -> next = temp; if (p == last) last = temp; return last; } p = p -> next; } while (p != last -> next); cout << item << " not present in the list." << endl; return last; }
  • 48. Deletion in Circular Linked List
  • 49. Deletion in Circular Linked List There are three situation for Deleting element in list. 1. Deletion at beginning. 2. Deletion at the middle. 3. Deletion at the end.
  • 50. Deletion - Algorithm Steps: • Create a circular linked list and assign reference of first node to head. • Input key to delete from user. Store it in some variable say key. Say key to delete is 20. • To keep track of previous node and node to delete, declare two variables of node type. Say cur = head and prev. Make sure previous points to last node. • If current node contains key, Then you got node to delete. • Before deleting a node, you must first adjust previous node link. • Adjust head node if needed. • Delete the node. • Update current node, i.e. assign cur = previous->next if previous != NULL. Otherwise assign NULL. • If current node does not contain key to delete, then simply update previous and current node. • Repeat step 3-4 till last node.
  • 51. Representation Deletion at the middle of the Circular linked list
  • 53. Representation Deletion at the middle of the Circular linked list
  • 55. Representation Deletion at the End of the Circular linked list
  • 57. Implementation • void delete_first(struct link *node) { • • node=start->next; • • ptr=start; • • if(i==0) { • • printf("n List is empty"); • exit(0); • } • • ptr->next=node->next; • free(node); • • i--; • }
  • 58. Summary • Introduction to Double Linked List • Insertions and Deletions in Doubly Linked List • Introduction to Circular Linked List • Insertion and Deletion in Circular Linked List
  • 59. References • https://www.geeksforgeeks.org/doubly-linked-list/ • http://scanftree.com/Data_Structure/Deletion-in- circular-linked-list • https://www.tutorialspoint.com/data_structures_alg orithms/circular_linked_list_algorithm.htm • https://codeforwin.org/2018/06/c-program-to- delete-element-from-circular-linked-list.html • https://www.cse.unr.edu/~bebis/CS308/PowerPoint/ LinkedDoublyLists.ppt