Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include struct Node { int val; struct Node *next; }; void display_list(struct Node *head); struct Node * destroy_list(struct Node *head); struct Node *create_list(int start_val,

image text in transcribed

#include #include

struct Node { int val; struct Node *next; };

void display_list(struct Node *head); struct Node * destroy_list(struct Node *head); struct Node *create_list(int start_val, int finish_val);

struct Node *delete(struct Node *head, int value);

void main() {

struct Node *p = create_list(3,13);

p = create_list(3,13); printf(" Created a list: "); display_list(p); p = delete(p, 7); printf("Deleted 7: "); display_list(p); p = delete(p, 13); printf("Deleted 13: "); display_list(p);

p = destroy_list(p); }

struct Node *delete(struct Node *head, int value) { // Node is empty if (head==NULL) { return NULL; } if(head->value == value) { delete(head = head->value) free(head); 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(" "); }

struct Node * destroy_list(struct Node *head) { struct Node *p = head; while (p!=NULL) { struct Node *next = p->next; free(p); p = next; } return NULL; }

struct Node *create_list(int start_val, int finish_val) { struct Node *head = NULL; /* Head of the list */ struct Node *last = NULL; /* Last node in the list */ for (int i=start_val; ival = i; p->next = NULL; if (i == start_val) { head = p; } else { last->next = p; } last = p; } return head; }

Problem 2 [2 pts]. Attached with this homework is a file "delete.c" that has the following function: struct Node delete(struct Node head, int value) This function deletes the first node in a linked list with the value "value". A similar function can be found in the lecture notes on Simple Data Structures, slide 13, that is implemented using the iterative method. Implement delete so it is a recursive function. Example recursive functions can be found in the lecture notes on Simple Data Structures, slide 12. Your algorithm should run in O(n) time, where n is the length of the list. The space complexity will be O(n) due to recursive functions will liberally use the stack

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions