Table of Contents
A C program needs to store integers. The size of this data is not known until execution time. To optimize memory usage, a linked list data structure was chosen. Each element of that list is a structure that contains a number and a pointer to the following element. The following figure shows an example of this structure.
The list is managed in the program simply by the pointer to its first element. Write the following code portions (you will need help with some of them, so we expect you to use the course forum):
Define the required data structure. How is the empty list represented?
Function receives no parameters and returns an empty list (it should not have more than one line of code).
Function that, given an integer, returns a list with a single element with this integer.
Function that, given a list and an integer, adds the integer to the list (the place in the list where the integer is inserted is irrelevant).
Function that returns the number of elements in a list.
Function that, given a list and an integer, removes all appearances of this element on the list (if any).
Function that, given two lists, returns the concatenation of both lists.
Function that deletes completely the content of the list.
Write these structures and functions in a file with its
own main
function and include in it calls to verify that your
data structure works correctly.
Decide first how are you going to represent the list and how are you going to store the elements. Think if the proposed structure is the appropriate one to be manipulated by the required functions.
For each function, first think about its prototype, that is, the result type and type and number of required parameters. Once this is clear, write the body of the function.
Once all functions have been implemented, move the
main
function to a separate file. Create a file with
extension “*.h
” containing the
definitions and prototypes needed to be included in the file with the
“main
” and compile without any
warnings.
Now, we will work with a list of students (of variable size) where, for each student, we will store her/his name (variable size), her/his scored (in another list of variable size) and her/his student id (an integer).
Choose the correct data definition for the scores of the students
Choose the correct data definition
We would like to initialize the students list in the main program as an empty list. Choose the correct data declaration.
We would like to implement a function that, given an id and the name of a student, returns a list with one element. The score list of this element will be empty. The function must copy the name of the student. Choose the correct code.
We would like to invoke the previous function. Choose the correct code.
We would like to implement a function that, given a list, an id and the name of a student, modifies the list, adding the new student at its end. The function must returns the new length of the list. Choose the correct prototype.