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

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;

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