SlideShare a Scribd company logo
1 of 19
Singly Linked List
A Linear Data Structure
SONI GUPTA
ASSISTANT PROFESSOR (NPIU-TEQIP III )
HBTU KANPUR, UP
TEQIP
-III
Deletion operation
TEQIP
-III
Deletion of a node
1. Delete first node
2. Delete last node
3. Delete node from given position
TEQIP
-III
Delete the first node
 Check if the linked list is empty
 If so return underflow
 Search for the node
 Find its successor and predecessor of the node
 If the end of the list has been reached then return node dose not exist
 Delete the node from the list
 Free the space (deleted node)
TEQIP
-III
 head
10 20
30 40
TEQIP
-III
 ptr head
10 20
30 40
TEQIP
-III
head=head->next
p->next=NULL
void delete_from_first()
{
struct node *ptr;
if(head == NULL)
{
printf("nList is emptyn");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("nNode deleted from the beginning of the linked list");
}
}
TEQIP
-III
Delete the last node
 Check if the linked list is empty
 If so return underflow
 Traverse the list to reach at the predecessor of last node
 Delete the node from the list
 Free the space (deleted node)
TEQIP
-III
 head
10 20
30 40
TEQIP
-III
 Head ptr1 ptr
10 20
30 40
TEQIP
-III
void delete_from_End(node head)
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("nlist is empty");
}
else if(head→ next == NULL)
{
head = NULL;
free(head);
printf("nOnly node of the list deleted ...n");
}
TEQIP
-III
else
{
ptr = head;
while(ptr → next != NULL)
{
ptr1 = ptr;
ptr = ptr → next;
}
ptr1 → next = NULL;
free(ptr);
printf("nDeleted Node from the last ...n");
}
}
TEQIP
-III
Delete the node from given position
1. Check if the linked list is empty
If so return underflow
2. Search for the node
3. Find its successor and predecessor of the node
4. If the end of the list has been reached then return node dose not exist
5. Delete the node from the list
6. Free the space (deleted node)
TEQIP
-III
 head
10 20
30 40
TEQIP
-III
 head
10 20
30 40
TEQIP
-III
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("n Enter the position of the node after which you want to perform deletion n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr→ next;
if(ptr == NULL)
{
printf("nGiven position does not exist");
return;
}
}
ptr1 → next = ptr → next;
free(ptr);
printf("nDeleted node %d ",loc+1);
}
TEQIP
-III
Search an element in a
given linked list
TEQIP
-III
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("nEmpty Listn");
}
else
{
printf("nEnter item which you want to search?n");
scanf("%d",&item);
TEQIP
-III
while (ptr!=NULL)
{
if(ptr → data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr → next;
}
if(flag==1)
{
printf("Item not foundn");
}
}
}
TEQIP
-III

More Related Content

What's hot

Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationProf Ansari
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Stack data structure
Stack data structureStack data structure
Stack data structureSoumyajit Pal
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Adam Mukharil Bachtiar
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by DivyaDivya Kumari
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by DivyaDivya Kumari
 
linked list
linked listlinked list
linked listAbbott
 
10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and QueuesPraveen M Jigajinni
 

What's hot (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Single linked list
Single linked listSingle linked list
Single linked list
 
1. 4 Singly linked list deletion
1. 4 Singly linked list deletion1. 4 Singly linked list deletion
1. 4 Singly linked list deletion
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
linked list
linked listlinked list
linked list
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
1. 6 doubly linked list
1. 6 doubly linked list1. 6 doubly linked list
1. 6 doubly linked list
 
Linked list
Linked listLinked list
Linked list
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
linked list
linked listlinked list
linked list
 
1. 5 Circular singly linked list
1. 5 Circular singly linked list1. 5 Circular singly linked list
1. 5 Circular singly linked list
 
10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues10 Linked Lists Sacks and Queues
10 Linked Lists Sacks and Queues
 
Doubly Link List
Doubly Link ListDoubly Link List
Doubly Link List
 

Similar to Linked list part-3

CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.pptArifKamal36
 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptxSaralaT3
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptxchin463670
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversalkasthurimukila
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfezzi97
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.pptSLekshmiNair
 
Link list part 2
Link list part 2Link list part 2
Link list part 2Anaya Zafar
 
How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdffootstatus
 

Similar to Linked list part-3 (20)

2.ppt
2.ppt2.ppt
2.ppt
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Ds notes
Ds notesDs notes
Ds notes
 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.ppt
 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptx
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
STACKS AND QUEUES CONCEPTS
STACKS AND QUEUES CONCEPTSSTACKS AND QUEUES CONCEPTS
STACKS AND QUEUES CONCEPTS
 
Linked list
Linked listLinked list
Linked list
 
Circular_Linked_List.ppt
Circular_Linked_List.pptCircular_Linked_List.ppt
Circular_Linked_List.ppt
 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 

Recently uploaded

Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualBalamuruganV28
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024EMMANUELLEFRANCEHELI
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docxrahulmanepalli02
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfJNTUA
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdfKamal Acharya
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...IJECEIAES
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
Intro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney UniIntro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney UniR. Sosa
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentationsj9399037128
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New HorizonMorshed Ahmed Rahath
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...IJECEIAES
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Toolssoginsider
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...Amil baba
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 

Recently uploaded (20)

Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Intro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney UniIntro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney Uni
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 

Linked list part-3

  • 1. Singly Linked List A Linear Data Structure SONI GUPTA ASSISTANT PROFESSOR (NPIU-TEQIP III ) HBTU KANPUR, UP TEQIP -III
  • 3. Deletion of a node 1. Delete first node 2. Delete last node 3. Delete node from given position TEQIP -III
  • 4. Delete the first node  Check if the linked list is empty  If so return underflow  Search for the node  Find its successor and predecessor of the node  If the end of the list has been reached then return node dose not exist  Delete the node from the list  Free the space (deleted node) TEQIP -III
  • 5.  head 10 20 30 40 TEQIP -III
  • 6.  ptr head 10 20 30 40 TEQIP -III head=head->next p->next=NULL
  • 7. void delete_from_first() { struct node *ptr; if(head == NULL) { printf("nList is emptyn"); } else { ptr = head; head = ptr->next; free(ptr); printf("nNode deleted from the beginning of the linked list"); } } TEQIP -III
  • 8. Delete the last node  Check if the linked list is empty  If so return underflow  Traverse the list to reach at the predecessor of last node  Delete the node from the list  Free the space (deleted node) TEQIP -III
  • 9.  head 10 20 30 40 TEQIP -III
  • 10.  Head ptr1 ptr 10 20 30 40 TEQIP -III
  • 11. void delete_from_End(node head) { struct node *ptr,*ptr1; if(head == NULL) { printf("nlist is empty"); } else if(head→ next == NULL) { head = NULL; free(head); printf("nOnly node of the list deleted ...n"); } TEQIP -III
  • 12. else { ptr = head; while(ptr → next != NULL) { ptr1 = ptr; ptr = ptr → next; } ptr1 → next = NULL; free(ptr); printf("nDeleted Node from the last ...n"); } } TEQIP -III
  • 13. Delete the node from given position 1. Check if the linked list is empty If so return underflow 2. Search for the node 3. Find its successor and predecessor of the node 4. If the end of the list has been reached then return node dose not exist 5. Delete the node from the list 6. Free the space (deleted node) TEQIP -III
  • 14.  head 10 20 30 40 TEQIP -III
  • 15.  head 10 20 30 40 TEQIP -III
  • 16. void random_delete() { struct node *ptr,*ptr1; int loc,i; printf("n Enter the position of the node after which you want to perform deletion n"); scanf("%d",&loc); ptr=head; for(i=0;i<loc;i++) { ptr1 = ptr; ptr = ptr→ next; if(ptr == NULL) { printf("nGiven position does not exist"); return; } } ptr1 → next = ptr → next; free(ptr); printf("nDeleted node %d ",loc+1); } TEQIP -III
  • 17. Search an element in a given linked list TEQIP -III
  • 18. void search() { struct node *ptr; int item,i=0,flag; ptr = head; if(ptr == NULL) { printf("nEmpty Listn"); } else { printf("nEnter item which you want to search?n"); scanf("%d",&item); TEQIP -III
  • 19. while (ptr!=NULL) { if(ptr → data == item) { printf("item found at location %d ",i+1); flag=0; } else { flag=1; } i++; ptr = ptr → next; } if(flag==1) { printf("Item not foundn"); } } } TEQIP -III