Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method that will delete from a linked list of integers the node that con

ID: 3620191 • Letter: W

Question

Write a method that will delete from a linked list of integers the node that contains the largest integer. Can you do this with a single traversal of the list?

Explanation / Answer

#include struct node{ int value; struct node *next; }*head; void insert(){ int no; struct node *temp; printf(" Enter a number: "); scanf("%d",&no); temp = (struct node *)malloc(sizeof(struct node)); temp->value=no; temp->next=NULL; if(head==NULL){ head=temp; } else{ temp->next=head; head=temp; } } void delete2(){ struct node * temp, *maxnode,*parentnode; parentnode=NULL; maxnode=head; temp=head; if(head!=NULL){ while(temp->next!=NULL){ if(maxnode->valuenext->value){ parentnode=temp; maxnode=temp->next; } temp=temp->next; } if(parentnode!=NULL) parentnode->next=parentnode->next->next; else head=head->next; if(head!=NULL&&head->next==NULL&&parentnode==NULL){ free(maxnode); // head=NULL; } else if(head!=NULL){ free(maxnode); } } } void print(struct node *temp){ while(temp!=NULL){ printf("-->%d",temp->value); temp=temp->next; } } int main(){ int ch=1; while(1){ printf(" 1.Insert 2.Delete 3.Print 4.Exit Enter choice:"); scanf("%d",&ch); switch(ch){ case 1: insert();break; case 2: delete2();break; case 3: print(head);break; case 4: exit(0); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote