Question
Language is C Modify deleteCommandList() so that it now also frees all the nodes that are in a loop command. The code works if the
Language is C
Modify deleteCommandList() so that it now also frees all the nodes that are in a loop command. The code works if the cmdnode struct does not include the loop_head struct node.
The function is suppsosed to delete the entire linked list and setst it to NULL pretend the cmdnode struct does not include the loop_head struct node and it works please make the function work with the loop_head struct. Here is an example of a list you are deleting.
0: forward 100 1: loop 4 2: forward 10 //(inside loop struct) 3: left //(inside loop struct) 4: right //(back outside of loop struct)
The function will delete everything in this example except for what is outside the loop (does not delete forward 10 and left)
Valid commands for cmd are penup, pendown, forward, backward, left, right (forward, backward and colour have a value for val the rest have 0 for val)
#include #include #include
/** * This struct contains one node of the linked list, which represents a single * command to the Turtle. It's field should include: * * - cmd : A char array of size 10 holding the command name * * - val : An integer that stores a parameter for the command (like forward, * backward and colour). *
* 'loop_head' to the CmdNode struct which is a pointer to a struct of the same type. This will be the head of the linked list of commands that are in the loop. It should be set to NULL for commands of any other type. * - next : A pointer to a struct of the same type, this is used for the * linked list * * ****/
typedef struct cmdnode { char cmd[10]; int val; struct cmdnode *loop_head; struct cmdnode *next; } CmdNode;
CmdNode *deleteCommandList(CmdNode *head) { /** * This function deletes the linked list of commands, releasing all the * memory allocated to the nodes in the linked list. * * Returns a NULL pointer so that the head of the list can be set to NULL * after deletion. * * TODO: Implement this function */ CmdNode *temp = NULL; CmdNode *next_node = NULL;
if (head == NULL){ return NULL; } temp = head; while(temp!=NULL){ next_node = temp->next; free(temp); temp = next_node; } head = NULL; return head; }
//End of code
Do NOT include any other C libraries
Do NOT add any new perameters to functions
Do NOT use any built in functions other than ones that are used for strings (eg , strcmp, strcpy, printf etc)
Thank you :)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started