Question: Implement a linked list containing integers. To do this, you need a struct that implements a node in the list (with its value, and pointer

Implement a linked list containing integers. To do this, you need a struct that implements a node in the list (with its value, and pointer to the next node). Also make a typedef for your struct .(In contrast, you don't need to use void* for the value of the node, but can use an int , as this only needs to be in list of intars.) You should also have another struct , which represents the list itself. All it has to contain is a pointer to the first node of the list, but it can also contain a variable that stores the number of nodes in the list. Also, implement a function that adds a list node first to the list, as declared in Code Listing 1. Don't forget to check that a new list element was allocated in your function (that is, the call to malloc succeeded). After implementing this, you should also be able to implement a main as per Code Listing 2. To do something more with your list implementation, you also need to have a way to loop through the list from start to finish and do something with each element. Implement a print_list function that prints the contents of a list that you constructed with the code above. Your function may start with the head of the list, and then print the contents of each node and follow the next pointer to the next node, until it reaches a next pointer that is NULL and thus points to an empty list.

codlistning 1;

/* push_front * add a number first to element

* Parametrar: * list Pointer to a list. It may be empty. After the call 'list' will be one element longer than before

* data The new value.

* Return: * 0 if a list node could not be created. otherwise 1 */ int push_front(int_list * list, int data);

code listning2:

int main(){ int_list L; L.head = NULL; //initialize the list. It's empty so the list is empty

if(!push_front(&L, 10)) printf("couldn't push "); if(!push_front (&L,20)) printf("couldn't push "); if(!push_front(&L, &30)) printf("couldn't push "); // Now the list is[30,20,10] }

Testfprogram: You do this with advantage using recursion. The base case is an empty list (then nothing is printed). The recursive case prints the first element of the list and makes a recursive call to print the rest. After implementing the print function, write a program that reads in a sequence of numbers from the user, builds a list of them, and prints the list to the screen (separated by spaces). Your program should read in the numbers via the command line (i.e. via argc and argv ). You can use the function atoi from stdlib.h to convert from a string to an integer. So with push_front the result should be as follows. ./task_1 10 20 30 30 20 10 (output)

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!