SlideShare a Scribd company logo
DATA STRUCTURES
Dr. P. Subathra
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 4
21.08.2020
(10:00 AM – 11:00 AM
&
11:30 AM – 12:30 PM)
UNIT 1
SINGLY LINKED LIST
DELETION OPERATIONS
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
LOOK CLOSER……
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
LOOK CLOSER……
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
free()
HEAP
Operations on a Singly Linked List
• Creating a new list
• Insertion
- at the beginning
- at the end
- after and element
- before an element
• Deletion
- at the beginning
- at the end
- a given element
- at a position
• Display
• Search Element
• Reverse List
• Sort List
• Find Duplicates
• ………………
DELETE FIRST
ALGORITHM
Step 1 : Check if the list is empty,
if so, display a message and exit
Step 2: Else, store the data value
of the first nodes in a variable for
returning.
Step 3 : Make the header to point
to the next field of the first node.
Step 4 : Delete the first node to
free the memory occupied by it.
SAME ALGORITHM WORKS
• Case 1
– Delete from an Empty List
• Case 2
– Delete from a Non Empty List
DELETE FIRST
ALGORITHM & CODE
Step 1 : Check if the list is
empty, if so, display a message
and exit
ILLUSTRATION
DELETE FIRST (Empty List)
If (head == NULL)
{
printf(“Empty List, ignore the return
value”);
return (-999);
}
ALGORITHM & CODE
Step 2: Else, store the data value of
the first nodes in a variable for
returning.
Step 3 : Make the header to point to
the next field of the first node.
ILLUSTRATION
DELETE FIRST : Non Empty List (Single Node)
else
{ // X is an integer variable
X= headdata;
head=headnext
return (X);
}
Empty list
5X
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
ALGORITHM & CODE
Step 2: Else, store the data value of the first
nodes in a variable for returning.
Step 3 : Make the header to point to the next
field of the first node.
Step 4 : Delete the first node to free the
memory occupied by it.
ILLUSTRATION
else
{
X= headdata; // X is an integer variable
node * temp = head;
head=headnext;
free(temp);
temp=NULL;
return (X);
}
Empty list
5
X
temp
1000
NULL
temp
DELETE FIRST : Non Empty List (Single Node)
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1036
temp
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
HEAP
1036
temp
NULL
ALGORITHM & CODE
Step 2: Else, store the data value of the first
nodes in a variable for returning.
Step 3 : Make the header to point to the
next field of the first node.
Step 4 : Delete the first node to free the
memory occupied by it.
ILLUSTRATION
DELETE FIRST : Non Empty List (Multiple Nodes)
else
{
X= headdata; // X is an integer variable
node * temp = head;
head=headnext;
free(temp);
temp=NULL;
return (X);
}
5 X
temp
1000
NULL
temp
DELETE LAST
DELETE LAST
Step 1 : Check if the list is empty,
if so, display a message and exit
Step 2: Else, if only one node
available in the list, store the
data value of that nodes in a
variable for returning.
Step 3 : Make the header NULL
Step 4 : Delete the first node to
free the memory occupied by it.
SAME ALGORITHM WORKS
• Case 1
– Delete an Empty List
• Case 2
– Delete from a Non Empty List
Operations on a Singly Linked List
ALGORITHM & CODE
Step 1 : Check if the list is
empty, if so, display a message
and exit
ILLUSTRATION
DELETE LAST : Empty List
If (head == NULL)
{
printf(“Empty List, ignore the return
value”);
return (-999);
}
ALGORITHM & CODE
Step 2:
(i) Else, IF ONLY ONE NODE is in the list, store the
data value of the first nodes in a variable for
returning.
(ii) Make the header to point to the next field of
the first node.
(iii) Delete the first node to free the memory
occupied by it.
ILLUSTRATION
DELETE LAST (Non Empty List - Single Node)
else if (headnext==NULL)
{
X= headdata; // X is an integer variable
node * temp = head;
head=headnext;
free(temp);
temp=NULL;
return (X);
} Empty list
5
X
temp
1000
NULL
temp
ALGORITHM & CODE
Step 3:
(i) Else, traverse till the last node keeping track
of the previous node. (Eg. 8)
ILLUSTRATION
else
{
node * previous=head;
node * current = head-> next;
while (current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
}
DELETE LAST: (MULTIPLE NODES)
ALGORITHM & CODE
STEP 3:
ii. Store the value pointed by CURRENT
in a variable for returning.
iii. Make NEXT field of PREVIOUS
node
to be NULL
iv. Free the memory occupied by the
last node.
ILLUSTRATION
DELETE LAST (MULTIPLE NODES)
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = element
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
X = currentdata;
previousnext =NULL;
free(current);
current=NULL;
}
8X
NULL
NULL
DELETE ELEMENT
ALGORITHM & CODE
Step 1 : Check if the list is
empty, if so, display a message
and exit
ILLUSTRATION
DELETE ELEMENT (Empty List)
If (head == NULL)
{
printf(“Empty List, Element not available
ignore the return value”);
return (-1);
}
ALGORITHM & CODE
Step 2:
Else, IF ONLY ONE NODE is in the list
(i) If header data is the search element,
Make header=NULL
(ii) Else print message “Element not
available”
ILLUSTRATION
DELETE ELEMENT (Non Empty List - Single Node)
else if (headnext==NULL)
{
if(headdata==searchElement) {
node * temp=head;
head=NULL;
free(temp);
temp=NULL;
return(1);
}
else{
Printf(“Element not available”);
return(-1);
}
Empty list
5
Search
Element
temp
1000
NULL
temp
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified node is
reached or End of List is reached. While
traversing, keep track of the previous node
also.
Let searchElement = 7
ILLUSTRATION
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = searchElement ||
currentnext!=NULL)
{
current= current-> next;
previous = previous->next;
}
}
DELETE ELEMENT (Non Empty List - MULTIPLE NODES)
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified node is
reached or End of List is reached. While
traversing, keep track of the previous node
also.
Let searchElement = 7
ILLUSTRATION
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = searchElement
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
prevnext=currentnext;
free(current);
current=NULL;
}
DELETE ELEMENT(Non Empty List - MULTIPLE NODES)
NULL
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified node is
reached or End of List is reached. While
traversing, keep track of the previous node
also.
Let searchElement = 7
ILLUSTRATION
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = searchElement ||
currentnext!=NULL)
{
current= current-> next;
previous = previous->next;
}
}
DELETE ELEMENT (Non Empty List - MULTIPLE NODES)
NULL
NULL
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified node is
reached or End of List is reached. While
traversing, keep track of the previous node
also.
Let searchElement = 7
ILLUSTRATION
else
{
node * previous=head;
node * current = head-> next;
while (current->data! = searchElement
|| current->next!=NULL)
{
current= current-> next;
previous = previous->next;
}
prevnext=currentnext;
free(current);
current=NULL;
}
DELETE ELEMENT(Non Empty List - MULTIPLE NODES)
NULL
NULL
NULL NULL
DELETE GIVEN POSITION
ALGORITHM & CODE
Step 1 : Check if the list is
empty, if so, display a message
and exit
ILLUSTRATION
DELETE POSITION (Empty List)
If (head == NULL)
{
printf(“Empty List, Element not available
ignore the return value”);
return (-1);
}
ALGORITHM & CODE ILLUSTRATION
DELETE POSITION (Non Empty List - Single Node)
else if (k == 1)
{
node * temp=head;
int X = headdata;
head= headnext;
free(temp);
temp=NULL;
return(X);
}
Empty list
5
Element
at Position
1000
tempX
Step 2: Let k = POSITION
Else if (k== 1)
(i) Store the element of the first node
to return
(ii) Make header to point to the second
node
(iii) Free the memory occupied by first
node
1000
temp
NULL
NULL
ALGORITHM & CODE
Step 2: Let k = POSITION
Else if (k== 1)
(i) Store the element of the first node
to return
(ii) Make header to point to the second
node
(iii) Free the memory occupied by first
node
ILLUSTRATION
DELETE POSITION (Non Empty List -Multiple Nodes)
else if (k == 1)
{
node * temp=head;
int X = headdata;
head= headnext;
free(temp);
temp=NULL;
return(X);
}
5
1000
X
temp
temp
NULL
ALGORITHM & CODE
STEP 3:
iii. Else Traverse the list until the specified
POSITION is reached or End of List is reached.
While traversing, keep track of the previous
node also.
Let, POSITION =3
ILLUSTRATION
else
{
count =2,
node * previous=head;
node * current = head-> next;
while (count < POSITION ||
currentnext!=NULL)
{
current= current-> next;
previous = previous->next;
count++;
}
}
DELETE POSITION (Non Empty List - MULTIPLE NODES)
Count =2
Count =3
STEP 3:
iv. If position is a valid position, store the value
of that node for returning
v. Make the previous node to point to the
POSITION node’s next pointer value
ILLUSTRATION
else
{
count =2,
node * previous=head;
node * current = head-> next;
while (count < POSITION || currentnext!=NULL)
{
current= current-> next;
previous = previous->next;
count++;
}
if (count == POSITION)
{
prevnext=currentnext;
X = currentdata;
free(current);
current=NULL;
return (X);
}
}
DELETE POSITION (Non Empty List - MULTIPLE NODES)
NULL
ALGORITHM & CODE
STEP 3:
vi. Else if POSITION is not available, Print a
message and return -1.
Eg. POSITION = 5
ILLUSTRATION
else
{
count =2,
node * previous=head;
node * current = head-> next;
while (count < POSITION || currentnext!=NULL)
{
current= current-> next;
previous = previous->next;
count++;
}
if (count == POSITION)
{
prevnext=currentnext; X = currentdata;
free(current); current=NULL; return (X);
}
else
{ Printf (“Position not available”);
return (-1);
} }
DELETE POSITION (Non Empty List - MULTIPLE NODES)
ALGORITHM & CODE
Count =5
END OF
DELETION OPERATIONS
PRINT THE LIST

More Related Content

What's hot (20)

PPTX
Data structure by Digvijay
Digvijay Singh Karakoti
 
PPTX
Stack_Data_Structure.pptx
sandeep54552
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PPT
Stacks, Queues, Deques
A-Tech and Software Development
 
PPT
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
PDF
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
PDF
stacks and queues
DurgaDeviCbit
 
PPT
Stacks
Malainine Zaid
 
PPT
Stack & queue
Siddique Ibrahim
 
PPTX
Stack of Data structure
Sheikh Monirul Hasan
 
PPTX
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
PPTX
Introduction to stack
vaibhav2910
 
PPT
Stacks overview with its applications
Saqib Saeed
 
PPTX
Stacks in Data Structure
Lovely Professional University
 
PPTX
Stack and Queue
Apurbo Datta
 
PPT
23 stacks-queues-deques
Rishabh Jindal
 
PPTX
Stack - Data Structure
Bhavesh Sanghvi
 
PPTX
Stack in Sata Structure
Muhazzab Chouhadry
 
PPT
Queue Data Structure
Sriram Raj
 
PPTX
Project of data structure
Umme habiba
 
Data structure by Digvijay
Digvijay Singh Karakoti
 
Stack_Data_Structure.pptx
sandeep54552
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Stacks, Queues, Deques
A-Tech and Software Development
 
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
stacks and queues
DurgaDeviCbit
 
Stack & queue
Siddique Ibrahim
 
Stack of Data structure
Sheikh Monirul Hasan
 
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
Introduction to stack
vaibhav2910
 
Stacks overview with its applications
Saqib Saeed
 
Stacks in Data Structure
Lovely Professional University
 
Stack and Queue
Apurbo Datta
 
23 stacks-queues-deques
Rishabh Jindal
 
Stack - Data Structure
Bhavesh Sanghvi
 
Stack in Sata Structure
Muhazzab Chouhadry
 
Queue Data Structure
Sriram Raj
 
Project of data structure
Umme habiba
 

Similar to 1. 4 Singly linked list deletion (20)

PPTX
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
PPTX
Linked list part-3
Soni Gupta
 
PDF
Data structure singly linked list programs VTU Exams
iCreateWorld
 
PPTX
VCE Unit 02 (1).pptx
skilljiolms
 
PPTX
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
PPTX
Linked lists a
Khuram Shahzad
 
PPTX
Presentation1.pptx
Koteswari Kasireddy
 
PPTX
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
PDF
algorithms_in_linkedlist (1).pdf
Koteswari Kasireddy
 
PPTX
Linked list data structures and algorithms
RaghavendraPrasad179187
 
PPTX
3.linked list
Chandan Singh
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPT
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
PPTX
List,Stacks and Queues.pptx
UmatulSaboohSaleem1
 
PPT
Chapter 5 ds
Hanif Durad
 
PPT
Operations on linked list
Sumathi Kv
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
ds 4Linked lists.ppt
AlliVinay1
 
PDF
Data Structure
Hitesh Mohapatra
 
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Linked list part-3
Soni Gupta
 
Data structure singly linked list programs VTU Exams
iCreateWorld
 
VCE Unit 02 (1).pptx
skilljiolms
 
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
Linked lists a
Khuram Shahzad
 
Presentation1.pptx
Koteswari Kasireddy
 
algorithms_in_linkedlist.pptx
Koteswari Kasireddy
 
algorithms_in_linkedlist (1).pdf
Koteswari Kasireddy
 
Linked list data structures and algorithms
RaghavendraPrasad179187
 
3.linked list
Chandan Singh
 
Unit ii(dsc++)
Durga Devi
 
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
List,Stacks and Queues.pptx
UmatulSaboohSaleem1
 
Chapter 5 ds
Hanif Durad
 
Operations on linked list
Sumathi Kv
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
ds 4Linked lists.ppt
AlliVinay1
 
Data Structure
Hitesh Mohapatra
 
Ad

More from P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai (20)

PPTX
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
1. C Basics for Data Structures Bridge Course
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
The stable marriage problem iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Maximum matching in bipartite graphs iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Knapsack dynamic programming formula top down (1)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Knapsack dynamic programming formula bottom up
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Multiplication of integers &amp; strassens matrix multiplication subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Multiplication of large integers problem subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
3.1 Trees ( Introduction, Binary Trees & Binary Search Trees)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
The stable marriage problem iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Maximum matching in bipartite graphs iterative improvement method
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Knapsack dynamic programming formula top down (1)
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Multiplication of integers &amp; strassens matrix multiplication subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Multiplication of large integers problem subi notes
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Ad

Recently uploaded (20)

PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 

1. 4 Singly linked list deletion

  • 1. DATA STRUCTURES Dr. P. Subathra Professor Dept. of Information Technology KAMARAJ College of Engineering & Technology
  • 2. CS8391 – DATA STRUCTURES ONLINE CLASSES – CLASS NO. 4 21.08.2020 (10:00 AM – 11:00 AM & 11:30 AM – 12:30 PM)
  • 5. HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 LOOK CLOSER……
  • 6. HEAP 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 LOOK CLOSER……
  • 7. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 8. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 9. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 10. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 11. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 12. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 free() HEAP
  • 13. Operations on a Singly Linked List • Creating a new list • Insertion - at the beginning - at the end - after and element - before an element • Deletion - at the beginning - at the end - a given element - at a position • Display • Search Element • Reverse List • Sort List • Find Duplicates • ………………
  • 15. ALGORITHM Step 1 : Check if the list is empty, if so, display a message and exit Step 2: Else, store the data value of the first nodes in a variable for returning. Step 3 : Make the header to point to the next field of the first node. Step 4 : Delete the first node to free the memory occupied by it. SAME ALGORITHM WORKS • Case 1 – Delete from an Empty List • Case 2 – Delete from a Non Empty List DELETE FIRST
  • 16. ALGORITHM & CODE Step 1 : Check if the list is empty, if so, display a message and exit ILLUSTRATION DELETE FIRST (Empty List) If (head == NULL) { printf(“Empty List, ignore the return value”); return (-999); }
  • 17. ALGORITHM & CODE Step 2: Else, store the data value of the first nodes in a variable for returning. Step 3 : Make the header to point to the next field of the first node. ILLUSTRATION DELETE FIRST : Non Empty List (Single Node) else { // X is an integer variable X= headdata; head=headnext return (X); } Empty list 5X
  • 18. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP
  • 19. ALGORITHM & CODE Step 2: Else, store the data value of the first nodes in a variable for returning. Step 3 : Make the header to point to the next field of the first node. Step 4 : Delete the first node to free the memory occupied by it. ILLUSTRATION else { X= headdata; // X is an integer variable node * temp = head; head=headnext; free(temp); temp=NULL; return (X); } Empty list 5 X temp 1000 NULL temp DELETE FIRST : Non Empty List (Single Node)
  • 20. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP 1036 temp
  • 21. 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 HEAP 1036 temp NULL
  • 22. ALGORITHM & CODE Step 2: Else, store the data value of the first nodes in a variable for returning. Step 3 : Make the header to point to the next field of the first node. Step 4 : Delete the first node to free the memory occupied by it. ILLUSTRATION DELETE FIRST : Non Empty List (Multiple Nodes) else { X= headdata; // X is an integer variable node * temp = head; head=headnext; free(temp); temp=NULL; return (X); } 5 X temp 1000 NULL temp
  • 24. DELETE LAST Step 1 : Check if the list is empty, if so, display a message and exit Step 2: Else, if only one node available in the list, store the data value of that nodes in a variable for returning. Step 3 : Make the header NULL Step 4 : Delete the first node to free the memory occupied by it. SAME ALGORITHM WORKS • Case 1 – Delete an Empty List • Case 2 – Delete from a Non Empty List Operations on a Singly Linked List
  • 25. ALGORITHM & CODE Step 1 : Check if the list is empty, if so, display a message and exit ILLUSTRATION DELETE LAST : Empty List If (head == NULL) { printf(“Empty List, ignore the return value”); return (-999); }
  • 26. ALGORITHM & CODE Step 2: (i) Else, IF ONLY ONE NODE is in the list, store the data value of the first nodes in a variable for returning. (ii) Make the header to point to the next field of the first node. (iii) Delete the first node to free the memory occupied by it. ILLUSTRATION DELETE LAST (Non Empty List - Single Node) else if (headnext==NULL) { X= headdata; // X is an integer variable node * temp = head; head=headnext; free(temp); temp=NULL; return (X); } Empty list 5 X temp 1000 NULL temp
  • 27. ALGORITHM & CODE Step 3: (i) Else, traverse till the last node keeping track of the previous node. (Eg. 8) ILLUSTRATION else { node * previous=head; node * current = head-> next; while (current->next!=NULL) { current= current-> next; previous = previous->next; } } DELETE LAST: (MULTIPLE NODES)
  • 28. ALGORITHM & CODE STEP 3: ii. Store the value pointed by CURRENT in a variable for returning. iii. Make NEXT field of PREVIOUS node to be NULL iv. Free the memory occupied by the last node. ILLUSTRATION DELETE LAST (MULTIPLE NODES) else { node * previous=head; node * current = head-> next; while (current->data! = element || current->next!=NULL) { current= current-> next; previous = previous->next; } X = currentdata; previousnext =NULL; free(current); current=NULL; } 8X NULL NULL
  • 30. ALGORITHM & CODE Step 1 : Check if the list is empty, if so, display a message and exit ILLUSTRATION DELETE ELEMENT (Empty List) If (head == NULL) { printf(“Empty List, Element not available ignore the return value”); return (-1); }
  • 31. ALGORITHM & CODE Step 2: Else, IF ONLY ONE NODE is in the list (i) If header data is the search element, Make header=NULL (ii) Else print message “Element not available” ILLUSTRATION DELETE ELEMENT (Non Empty List - Single Node) else if (headnext==NULL) { if(headdata==searchElement) { node * temp=head; head=NULL; free(temp); temp=NULL; return(1); } else{ Printf(“Element not available”); return(-1); } Empty list 5 Search Element temp 1000 NULL temp
  • 32. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. Let searchElement = 7 ILLUSTRATION else { node * previous=head; node * current = head-> next; while (current->data! = searchElement || currentnext!=NULL) { current= current-> next; previous = previous->next; } } DELETE ELEMENT (Non Empty List - MULTIPLE NODES)
  • 33. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. Let searchElement = 7 ILLUSTRATION else { node * previous=head; node * current = head-> next; while (current->data! = searchElement || current->next!=NULL) { current= current-> next; previous = previous->next; } prevnext=currentnext; free(current); current=NULL; } DELETE ELEMENT(Non Empty List - MULTIPLE NODES) NULL
  • 34. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. Let searchElement = 7 ILLUSTRATION else { node * previous=head; node * current = head-> next; while (current->data! = searchElement || currentnext!=NULL) { current= current-> next; previous = previous->next; } } DELETE ELEMENT (Non Empty List - MULTIPLE NODES) NULL NULL
  • 35. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified node is reached or End of List is reached. While traversing, keep track of the previous node also. Let searchElement = 7 ILLUSTRATION else { node * previous=head; node * current = head-> next; while (current->data! = searchElement || current->next!=NULL) { current= current-> next; previous = previous->next; } prevnext=currentnext; free(current); current=NULL; } DELETE ELEMENT(Non Empty List - MULTIPLE NODES) NULL NULL NULL NULL
  • 37. ALGORITHM & CODE Step 1 : Check if the list is empty, if so, display a message and exit ILLUSTRATION DELETE POSITION (Empty List) If (head == NULL) { printf(“Empty List, Element not available ignore the return value”); return (-1); }
  • 38. ALGORITHM & CODE ILLUSTRATION DELETE POSITION (Non Empty List - Single Node) else if (k == 1) { node * temp=head; int X = headdata; head= headnext; free(temp); temp=NULL; return(X); } Empty list 5 Element at Position 1000 tempX Step 2: Let k = POSITION Else if (k== 1) (i) Store the element of the first node to return (ii) Make header to point to the second node (iii) Free the memory occupied by first node 1000 temp NULL NULL
  • 39. ALGORITHM & CODE Step 2: Let k = POSITION Else if (k== 1) (i) Store the element of the first node to return (ii) Make header to point to the second node (iii) Free the memory occupied by first node ILLUSTRATION DELETE POSITION (Non Empty List -Multiple Nodes) else if (k == 1) { node * temp=head; int X = headdata; head= headnext; free(temp); temp=NULL; return(X); } 5 1000 X temp temp NULL
  • 40. ALGORITHM & CODE STEP 3: iii. Else Traverse the list until the specified POSITION is reached or End of List is reached. While traversing, keep track of the previous node also. Let, POSITION =3 ILLUSTRATION else { count =2, node * previous=head; node * current = head-> next; while (count < POSITION || currentnext!=NULL) { current= current-> next; previous = previous->next; count++; } } DELETE POSITION (Non Empty List - MULTIPLE NODES) Count =2 Count =3
  • 41. STEP 3: iv. If position is a valid position, store the value of that node for returning v. Make the previous node to point to the POSITION node’s next pointer value ILLUSTRATION else { count =2, node * previous=head; node * current = head-> next; while (count < POSITION || currentnext!=NULL) { current= current-> next; previous = previous->next; count++; } if (count == POSITION) { prevnext=currentnext; X = currentdata; free(current); current=NULL; return (X); } } DELETE POSITION (Non Empty List - MULTIPLE NODES) NULL ALGORITHM & CODE
  • 42. STEP 3: vi. Else if POSITION is not available, Print a message and return -1. Eg. POSITION = 5 ILLUSTRATION else { count =2, node * previous=head; node * current = head-> next; while (count < POSITION || currentnext!=NULL) { current= current-> next; previous = previous->next; count++; } if (count == POSITION) { prevnext=currentnext; X = currentdata; free(current); current=NULL; return (X); } else { Printf (“Position not available”); return (-1); } } DELETE POSITION (Non Empty List - MULTIPLE NODES) ALGORITHM & CODE Count =5