Question
In C only please !!! The purpose of this lab is to get your first exposure to implementing a linked list. Begin by implementing the
In C only please!!!
The purpose of this lab is to get your first exposure to implementing a linked list.
Begin by implementing the following function:
List * initIntegerList()
All this will do is return whatever you define to be an empty list, e.g., NULL, and will be called as
List *head; head = initIntegerList();
Next implement a function, insertInteger, that inserts a given integer at the head of a list. Youll need to determine the prototype you think is best. What will happen if theres insufficient memory to complete the insertion? How will the user know this has occurred?
The next function, printList(List *), can be called by the user to print the integers in a given list. Youll find this function useful for verifying that your insertion function is working properly.
And the final function, freeList, should free all memory allocated for a given list. Be sure to never access the content of any memory after its been freed! You may want to consider implementing it so that the users pointer is set to NULL in the process, i.e., to prevent the user from accidentally referencing freed memory afterward.
Once youre done with all of this, you may want to try re-implementing the functions to make use of a dummy node like I discussed in lecture (doing so is entirely optional and is neither required nor expected). Now initIntegerList will return a pointer to a list node instead of NULL, so you can implement insertInteger with a prototype like:
int insertInteger(int k, *list)
where the return value signifies whether the integer was successfully inserted. Note that you dont need to pass the list by reference because the value of the list will never change. This will become clear when you implement it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started