Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include struct Node { int val; struct Node *next; }; void display_list(struct Node *head); void destroy_list(struct Node *head); struct Node *create_list(int startVal, int finishVal);
#includeProblem 4 (not from the textbook). [2 pts] Attached with this homework is a file "list.c". The following functions do work: struct Node * reverse(struct node * head): Reverses the linked list and return its head Example: Input >L(0)>L(1)>L(2)>>L(n1)> NULL Output >L(n1)>L(n2)>.>L(1)>L(0)>NULL struct Node * remove_Kth_from_end(struct Node * head, int k); Removes the k-th node from the end of the linked list and return its head. Example: Input >L(0)>L(1)>L(2)>>L(n1)>NULL and k=2 Output >L(0)>L(1)>>L(n3)>L(n1)> NULL You may assume that k is valid, i.e., 1#include struct Node { int val; struct Node *next; }; void display_list(struct Node *head); void destroy_list(struct Node *head); struct Node *create_list(int startVal, int finishVal); struct Node *reverse(struct Node *head); struct Node *remove_Kth_from_end(struct Node *head, int k); void main() { struct Node *p = create_list(3,13); printf(" Created a list: "); display_list(p); p = reverse(p); printf("Reversed the list: "); display_list(p); destroy_list(p); p = create_list(3,13); printf(" Created a list: "); display_list(p); p =remove_Kth_from_end(p, 5); printf("Removed 5-th from end: "); display_list(p); destroy_list(p); } struct Node *reverse(struct Node *head) { return head; } struct Node *remove_Kth_from_end(struct Node *head, int k) { return head; } void display_list(struct Node *head) { printf("List: "); for (struct Node *p=head; p!=NULL; p=p->next) { printf("->%d ",p->val); } printf(" "); } void destroy_list(struct Node *head) { struct Node *p = head; while (p!=NULL) { struct Node *next = p->next; free(p); p = next; } return; } struct Node *create_list(int startVal, int finishVal) { struct Node *head = NULL; /* Head of the list */ struct Node *last = NULL; /* Last node in the list */ for (int i=startVal; ival = i; p->next = NULL; if (i == startVal) { head = p; } else { last->next = p; } last = p; } return head; }
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