Question: Exercise 1 Sample Input: 1 //List = 1 2 //List = 21 3 //List = 321 4 //List = 4321 Sample Output: My List: 4

Exercise 1

Exercise 1 Sample Input: 1 //List = 1 2 //List = 21

Sample Input: 1 //List = 1 2 //List = 21 3 //List = 321 4 //List = 4321 Sample Output: My List: 4 3 2 1 My List After Destroy: //should be empty

Skeleton Code:

#include #include //for malloc() and free()

//Declaration of a Linked List Node typedef struct NODE{ int data; struct NODE* next; } node; //Function prototypes node* addToHead(node*, int); void printList(node* ); void destroyList(node* );

int main() { //The skeleton of the input/output is coded for you //You are free to add code for debug purpose etc //Make sure the ouput matches with the sample given node* myList = NULL; //Empty List int input;

while (scanf("%i",&input) == 1){ //Add to head }

//Print out the linked list printf("My List: "); printList(myList); destroyList(myList); myList = NULL;

//Print out the linked list after destruction printf("My List After Destroy: "); printList(myList);

return 0; }

//Function Implementations node* addToHead(node* head, int newData) { //Fill in your code here

return NULL; //change this }

void printList(node* head) //Purpose: Print out the linked list content //Assumption: the list is properly null terminated {

//This function has been implemented for you node* ptr = head;

while (ptr != NULL) { //or you can write while(ptr) printf("%i ", ptr->data); ptr = ptr->next; } printf(" "); }

void destroyList(node* head) { //Fill in your code here }

This exercise requires you to write two functions: node* addToHead (node* list, int newValue); This function takes an existing linked list list and an integer value newValue. The function will: o Make a new node to store newValue o Make this new node to be the first node (head) of list o Return the modified list void destroyList (node* list); This function takes an existing linked list list and destroy every node in the linked list by return the memory to system. The program should: a. Read a new integer value b. Insert this integer value to the head position of linked list c. Go to step a, until user terminates input by pressing Ctrl-D o Ctrl-D is the end-of-file signal d. Print out the whole list e. Destroy the list f. Print out the list (should be empty!)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!