Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write all the code in the file list.c for all the required functions listed in list.h ____________________ list.h #include typedef struct node { double data;
Write all the code in the file list.c for all the required functions listed in list.h
____________________
list.h
#includetypedef struct node { double data; struct node *next; } Node; /* function: tests whether a list is empty input: head: pointer to the first node output: true if the list is empty and false otherwise */ bool IsEmpty(Node* head); /* function: inserts a new node with certain data after a certain position input: phead: pointer to the pointer to the first node index: the new node is inserted after position index insert a new node as the head if index is 0 x: data of the new node output: a pointer to the new node if insertion is successful, NULL otherwise */ Node* InsertNode(Node** phead, int index, double x); /* function: finds node with certain data input: head: pointer to the first node x: the first node whose data = x is returned output: returns the position of the first node whose data = x returns 0 if no such node exists */ int FindNode(Node* head, double x); /* function: deletes a node with certain data input: phead: pointer to the pointer to the first node x: the first node whose data = x is deleted output: returns the position of the deleted node returns 0 if no such node exists */ int DeleteNode(Node** phead, double x); /* function: prints all the nodes in the list input: head: pointer to the first node */ void DisplayList(Node* head); /* function: deletes all the nodes in the list and frees their memory input: head: pointer to the first node */ void DestroyList(Node* head);
_____________________________
list.c
#include#include #include #include "list.h" // Write all your functions here. // Testing all the functions: int main(void) { Node* head = NULL; // Output: true printf("%s ", IsEmpty(head) ? "true" : "false"); // Must output an empty line: DisplayList(head); // Must output "insert failed": Node* result = InsertNode(&head, 20, 7); printf("%s ", result == NULL ? "insert failed" : "insert succeeded"); // Output: 0.000000 1.000000 2.000000 3.000000 4.000000 for(int i = 0; i < 5; i++) { InsertNode(&head, i, i); } DisplayList(head); // Output: false printf("%s ", IsEmpty(head) ? "true" : "false"); // Output: 4.000000 3.000000 2.000000 1.000000 0.000000 0.000000 1.000000 2.000000 3.000000 4.000000 for(int i = 0; i < 5; i++) { InsertNode(&head, 0, i); } DisplayList(head); // Output: 0.000000 is at position 5 // 2.000000 is at position 3 // 4.000000 is at position 1 // 6.000000 is not in the list for(int i = 0; i < 7; i += 2) { int idx = FindNode(head, i); if(idx > 0) { printf("%f is at position %d ", (double)i, idx); } else { printf("%f is not in the list ", (double)i); } } // Output: inserted 10.000000 // 4.000000 3.000000 2.000000 10.000000 1.000000 0.000000 0.000000 1.000000 2.000000 3.000000 4.000000 Node *in = InsertNode(&head, 3, 10); printf("inserted %f ", in->data); DisplayList(head); // Output: 4.000000 3.000000 2.000000 10.000000 1.000000 0.000000 1.000000 2.000000 3.000000 4.000000 // 3.000000 2.000000 10.000000 1.000000 0.000000 1.000000 2.000000 3.000000 4.000000 // 3.000000 2.000000 10.000000 1.000000 0.000000 1.000000 2.000000 3.000000 DeleteNode(&head, 0); // Delete in the middle of the list. DisplayList(head); DeleteNode(&head, 4); // Delete at the front of the list. DisplayList(head); DeleteNode(&head, 4); // Delete at the end of the list. DisplayList(head); DestroyList(head); return 0; }
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