Shown below is the type definition for a node of a linked list. The function fn(
ID: 3625018 • Letter: S
Question
Shown below is the type definition for a node of a linked list. The function fn() has two input parameters: a pointer to the head of the list and a value. When fn() is called it searches for the first node having the same value and removes it from the list. If the value is not found there is no change in the list. Function returns the head of the list after the value is removed. Write the function fn().
typedef struct node{
int value;
struct node*next;
}node_t;
Any help will be greatly appreciated.
Another question: http://www.cramster.com/answers-apr-11/computer-science/function-question-lost-questionwrite-code-functio_1250130.aspx
Explanation / Answer
//not tested at all but this is the basic idea. node* fn(node* head, int val){ node* previous =0; node* temp = head; if(val = temp-> value ){ previous = temp->next; //in this case, previous is not previous, its next so i can save so // it saves the new head; delete temp; } else{ while(temp->next){ previous = temp; if(temp->next) temp = temp->next; if(val == temp->value){ previous->next = temp->next; delete temp; } } } return head; //pointer, so original head can be returned. }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.