Consider the following (incorrect) function which removes the head node in a glo
ID: 3916205 • Letter: C
Question
Consider the following (incorrect) function which removes the head node in a globally defined list called my_list. The struct grade_node is used to represent a node containing grade information in a linked list of grades.
struct grade_node {
int value;
char assignment[255];
struct grade_node* next;
};
struct grade_node* my_list = ... //assume this has been populated elsewhere.
void remove_head() {
if (my_list != NULL)
my_list = my_list.next;
}
a) What is the syntax error in remove_node?
b) What is the memory issue in remove_node?
Explanation / Answer
a) What is the syntax error in remove_node? my_list is of pointer type. So, To access next you have to use my_list->next instead of my_list.next b) What is the memory issue in remove_node? my_list = my_list.next; In the above statement, my_list is reallocated to my_node->next. But, Before doing this we have to free the memory of the current node my_list like below Code: ------ temp = my_list; my_list = my_list->next; temp->next = null;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.