C++ Quiz 11 Suggested point values are marked in parentheses. In all questions t
ID: 3717729 • Letter: C
Question
C++
Quiz 11
Suggested point values are marked in parentheses. In all questions that involve writing methods, be sure to think of whether method arguments and return values should be references to const data, whether or not the method should be const, etc.
The next two questions involve the following data types:
Also assume that current is a pointer to a node in the middle of a singly linked list, and that newNode is a pointer to a newly allocated node.
1. What is recursion?
2. Write a code fragment (not a complete method) that will insert newNode at the front of a non-empty list.
3. Write a code fragment that will insert newNode at the rear of a non-empty list.
4. Write a code fragment that will insert newNode into the list after the node pointed to by current.
5. Write a recursive method that will return the size of the list. The method should take a pointer to an Node<T> and return an int. When the method is initially called, it will be passed the list's head pointer.
6. Write a non-recursive method that will return the size of the list. The method should take no arguments and return an int.
7. Declare a pointer to a function that returns a bool and takes two double arguments.
8. Rewrite the contents of the following array once the array has been partitioned by the quicksort partition code used on Assignment 8.
26 10 67 30 18 54 41 28Explanation / Answer
Please find my answer.
I have answered first 5 Questions. As per the chegg guideline, please post only 4 part in a post.
Please repost others in separate post.
What is recursion?
Recursion is function call with a code such that, it calls to itself may be directly or indirectly.
Write a code fragment (not a complete method) that will insert newNode at the front of a non-empty list.
Node <T> temp; //Creates a new node.
temp->data = data; //Updates the data of the node.
temp->next = head; //Points the newly created node->next to the first node.
head = temp; //head now points to the newly created node.
Write a code fragment that will insert newNode at the rear of a non-empty list.
Node <T> temp; //Creates a new node.
temp->data = data; //Updates the data of the node.
temp->next = tail->next; //Points the newly created node->next to NULL.
tail->next = temp; //head now points to the newly created node.
Write a code fragment that will insert newNode into the list after the node pointed to by current.
Node <T> temp; //Creates a new node.
temp->data = data; //Updates the data of the node.
temp->next = current->next; //Points the newly created node->next to the next node of current.
current->next = temp; //current node -> next now points to the newly created node.
Write a recursive method that will return the size of the list. The method should take a pointer to an Node<T> and return an int. When the method is initially called, it will be passed the list's head pointer.
int sizeOfList(Node<T> *head)
{
if(head == NULL) //If there are no nodes, the size is considered as 0.
return 0;
return 1 + sizeOfList(head->next); //If the size is more than that, return 1 + the remaining nodes.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.