Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following structure declaration for a linked list in C: struct node { int data; struct node* tail; }; typedef struct node Node; We

Consider the following structure declaration for a linked list in C:

struct node {

int data;

struct node* tail;

};

typedef struct node Node;


We represent linked lists as pointers to Node structs. Empty lists are represented with the null pointer. Non-empty lists are represented as a valid pointer to a Node, with the head data in the data field of the struct and the tail in the tail field of the struct. All lists will be assumed to be non-cyclic.

(a) Write a function to add an element to the head of the list with the following prototype. Node *cons(int n, Node *tail); [5 marks]

(b) Write a function to free the memory associated with a linked list, with the following prototype: void free_list(Node *list); [5 marks]

(c) Write a function to do an in-place list reversal. It should take a list as an argument, and return a list with the elements reversed. This function may not do any heap allocation, but may modify its input. It should have the following prototype: Node *reverse(Node *list);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

The detailed answer for the above question is provided below a ... 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

Recommended Textbook for

Java How To Program Early Objects

Authors: Paul Deitel, Harvey Deitel

11th Edition

9780134743356

More Books

Students also viewed these Programming questions