Question
Below is the content of linkedlist.h that contains the declaration of functions that you will implement in subsequent questions. //filename: linkedlist.h #ifndef LINKEDLIST_H #define LINKEDLIST_H
Below is the content of linkedlist.h that contains the declaration of functions that you will implement in subsequent questions. //filename: linkedlist.h #ifndef LINKEDLIST_H
#define LINKEDLIST_H #include node.h //IMPORTANT: In all the functions the linked-list may only contain //letters of the alphabet in either lower or upper case
//Precondition: The address of a valid LinkedList and a char value //that may be an alphabet in either lower or upper case //Postcondition: Adds a new node with data element set to value to the //end of the linked list.
void addToEndOfList(LinkedList*& list, char value);
//Precondition: A char array with a given length, not necessarily a //C-string but containing only letters of the alphabet //Postcondition: The address of a new LinkedList containing all the //characters of the input array in the same order, where each node of //the linked list contains one character of the array.
LinkedList* arrayToLinkedList(char* arr, int len);
//Precondition: The address of a valid LinkedList and a char value //that is an alphabet in either lower or upper case //Postcondition: Returns the number of occurrences of the given //alphabet in either lower or upper case in the linked list
//You must use an iterative implementation (loops). int countCharIterative(LinkedList* list, char value);
//Precondition: The address of the first node in a linked list and a //char value that is a letter of the alphabet in either lower or upper //case. //Postcondition: Returns the number of occurrences of the given char // value in the linked list using a recursive implementation.
int countCharHelper(Node* head, char value);
//Precondition: The address of a valid LinkedList and a char value //that is a letter of the alphabet //Postcondition: Returns the number of occurrences of the given //value in the linked list in either upper or lower case. This //function uses the helper function countCharHelper()
int countChar(LinkedList* list, char value); #endif
Consider the following function that has an INCORRECT implementation that tries to add a new node to the end of a linked-list (that may be empty). Answer the questions that follow.
void addToEndOfList(LinkedList*& list, char value){ Node* n = new Node; list->tail = n; n->data = value; n->next = list->tail; return; }
Assume that the function is called passing in mylist (shown above) and the character s as follows: addToEndOfList(mylist, s); Starting with the linked list shown above, draw a pointer diagram to show the state of memory right before the function addToEndOfList()returns. You must label your diagram with the variables: list, mylist, value, show all the nodes of the linked list and any new objects that are created by addToEndOfList(). You don't need to upload the diagram. Instead use it to answer the following questions
a. (1 pt) Where is the pointer n created (Stack or Heap)?
b. (1 pt) Does the function addToEndOfList() have a memory leak?
c. (6 pts) Provide a correct implementation of the function addToEndOfList()that works on any linked-list including an empty list.
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