SlideShare a Scribd company logo
1 of 3
Download to read offline
How to delete one specific node in linked list in C?
Thanks
Solution
#include
#include
struct node
{
int data;
struct node *next;
};
void deleteNode(struct node *head, struct node *n)
{
if(head == n)
{
if(head->next == NULL)
{
printf("There is only one node. The list can't be made empty ");
return;
}
head->data = head->next->data;
n = head->next;
head->next = head->next->next;
free(n);
return;
}
struct node *prev = head;
while(prev->next != NULL && prev->next != n)
prev = prev->next;
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
prev->next = prev->next->next;
free(n);
return;
}
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;
*head_ref = new_node;
}
void printList(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf(" ");
}
int main()
{
struct node *head = NULL;
push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);
printf("Given Linked List: ");
printList(head);
printf(" Deleting node %d: ", head->next->next->data);
deleteNode(head, head->next->next);
printf(" Modified Linked List: ");
printList(head);
printf(" Deleting first node ");
deleteNode(head, head);
printf(" Modified Linked List: ");
printList(head);
getchar();
return 0;
}

More Related Content

Similar to How to delete one specific node in linked list in CThanksSolu.pdf

in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
eyewaregallery
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
feroz544
 

Similar to How to delete one specific node in linked list in CThanksSolu.pdf (20)

137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Linked list
Linked listLinked list
Linked list
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Lab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdfLab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
3.linked list
3.linked list3.linked list
3.linked list
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdf
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Linked list
Linked list Linked list
Linked list
 

More from footstatus

Without using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdfWithout using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdf
footstatus
 
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdfwrite a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
footstatus
 
What is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdfWhat is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdf
footstatus
 
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdfa) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
footstatus
 

More from footstatus (20)

A storm warning siren emits sound uniformly in all directions. Along.pdf
A storm warning siren emits sound uniformly in all directions. Along.pdfA storm warning siren emits sound uniformly in all directions. Along.pdf
A storm warning siren emits sound uniformly in all directions. Along.pdf
 
Without using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdfWithout using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdf
 
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdfwrite a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
 
With regards to eipdemiology, match the statements in the right colum.pdf
With regards to eipdemiology, match the statements in the right colum.pdfWith regards to eipdemiology, match the statements in the right colum.pdf
With regards to eipdemiology, match the statements in the right colum.pdf
 
Which of these are temperamental dimensionsoptionsAvoidant, re.pdf
Which of these are temperamental dimensionsoptionsAvoidant, re.pdfWhich of these are temperamental dimensionsoptionsAvoidant, re.pdf
Which of these are temperamental dimensionsoptionsAvoidant, re.pdf
 
Which of the following can cause OLS estimators to be biased Hetero.pdf
Which of the following can cause OLS estimators to be biased  Hetero.pdfWhich of the following can cause OLS estimators to be biased  Hetero.pdf
Which of the following can cause OLS estimators to be biased Hetero.pdf
 
what is the relationship between capsular type of H. influenza and p.pdf
what is the relationship between capsular type of H. influenza and p.pdfwhat is the relationship between capsular type of H. influenza and p.pdf
what is the relationship between capsular type of H. influenza and p.pdf
 
What is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdfWhat is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdf
 
Water is essential to all living organisms. Discuss THREE properties .pdf
Water is essential to all living organisms. Discuss THREE properties .pdfWater is essential to all living organisms. Discuss THREE properties .pdf
Water is essential to all living organisms. Discuss THREE properties .pdf
 
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdftree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
 
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdfThe plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
 
The region labeled with the letter B is transcribed into mRNA True.pdf
The region labeled with the letter B is transcribed into mRNA  True.pdfThe region labeled with the letter B is transcribed into mRNA  True.pdf
The region labeled with the letter B is transcribed into mRNA True.pdf
 
The positron decay of 15 o goes directly to the ground state of,15 N;.pdf
The positron decay of 15 o goes directly to the ground state of,15 N;.pdfThe positron decay of 15 o goes directly to the ground state of,15 N;.pdf
The positron decay of 15 o goes directly to the ground state of,15 N;.pdf
 
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdfa) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
 
QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf
QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdfQuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf
QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf
 
provide a brief definition of self assemblySolutionMolecular s.pdf
provide a brief definition of self assemblySolutionMolecular s.pdfprovide a brief definition of self assemblySolutionMolecular s.pdf
provide a brief definition of self assemblySolutionMolecular s.pdf
 
Match the Statements below with bacteriological tests. Eosin-methyle.pdf
Match the Statements below with bacteriological tests.  Eosin-methyle.pdfMatch the Statements below with bacteriological tests.  Eosin-methyle.pdf
Match the Statements below with bacteriological tests. Eosin-methyle.pdf
 
Many non-scientists have focused their criticism of evolution on the.pdf
Many non-scientists have focused their criticism of evolution on the.pdfMany non-scientists have focused their criticism of evolution on the.pdf
Many non-scientists have focused their criticism of evolution on the.pdf
 
It has been observed that F_st increases with the distance between po.pdf
It has been observed that F_st increases with the distance between po.pdfIt has been observed that F_st increases with the distance between po.pdf
It has been observed that F_st increases with the distance between po.pdf
 
Let R be an integral domain and F a field such that R is a subring of.pdf
Let R be an integral domain and F a field such that R is a subring of.pdfLet R be an integral domain and F a field such that R is a subring of.pdf
Let R be an integral domain and F a field such that R is a subring of.pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

How to delete one specific node in linked list in CThanksSolu.pdf

  • 1. How to delete one specific node in linked list in C? Thanks Solution #include #include struct node { int data; struct node *next; }; void deleteNode(struct node *head, struct node *n) { if(head == n) { if(head->next == NULL) { printf("There is only one node. The list can't be made empty "); return; } head->data = head->next->data; n = head->next; head->next = head->next->next; free(n); return; } struct node *prev = head; while(prev->next != NULL && prev->next != n) prev = prev->next; if(prev->next == NULL) { printf(" Given node is not present in Linked List"); return; }
  • 2. prev->next = prev->next->next; free(n); return; } 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; *head_ref = new_node; } void printList(struct node *head) { while(head!=NULL) { printf("%d ",head->data); head=head->next; } printf(" "); } int main() { struct node *head = NULL; push(&head,3); push(&head,2); push(&head,6); push(&head,5); push(&head,11); push(&head,10); push(&head,15); push(&head,12); printf("Given Linked List: "); printList(head); printf(" Deleting node %d: ", head->next->next->data); deleteNode(head, head->next->next);
  • 3. printf(" Modified Linked List: "); printList(head); printf(" Deleting first node "); deleteNode(head, head); printf(" Modified Linked List: "); printList(head); getchar(); return 0; }