SlideShare a Scribd company logo
1 of 59
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 (20)

Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
linked list
linked list linked list
linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Single linked list
Single linked listSingle linked list
Single linked list
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Linked list
Linked listLinked list
Linked list
 
Doubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || AlgorithmsDoubly Linked List || Operations || Algorithms
Doubly Linked List || Operations || Algorithms
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 
Doubly circular linked list
Doubly circular linked listDoubly circular linked list
Doubly circular linked list
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
single linked list
single linked listsingle linked list
single linked list
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Binary tree
Binary tree Binary tree
Binary tree
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 

Similar to Double & Circular Linked Lists: Insertion and Deletion Operations Explained

Similar to Double & Circular Linked Lists: Insertion and Deletion Operations Explained (20)

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
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
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 ProcessingAfaq Mansoor Khan
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in PakistanAfaq Mansoor Khan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAfaq Mansoor Khan
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An OverviewAfaq Mansoor Khan
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design DecisionsAfaq Mansoor Khan
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinAfaq Mansoor Khan
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - AsteroidsAfaq Mansoor Khan
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting AlgorithmsAfaq 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

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 

Recently uploaded (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 

Double & Circular Linked Lists: Insertion and Deletion Operations Explained

  • 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