Linked List
6 questions · Fundamental Engineering
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
The function mergeTwoSortedList concatenates two linear sorted linked lists in a sorted order and returns one linear linked list of sorted order. Each element of the linear linked lists is represented by the class ListElement. The table lists the description of the class ListElement. ListElement-type variable holds a reference to an instance of the class ListElement. Here, head1 and head2 represent the headers of the two sorted linked lists, respectively and are of type ListElement. After concatenation of two existing linear linked lists, the function returns a linear linked list headed with result, which is of type ListElement. Both the input linked lists and returned linked list are sorted in ascending order of the member variable val.
Table Explanation of the member variables of the class ListElement
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
A singly linked list can be reversed using the following recursive procedure.
Let head be the first element of the list, and let elm be undefined.
(a) Reverse the order of the elements in the list starting from head, and set the next element of the last element in the reversed list—that is, the next element of head—to elm.
To “reverse the order of the elements in the list starting from head,” reverse the list starting from the next element of head, and then connect head to the end of the reversed list. In other words, execute (a) by setting head as elm, and the next element of head as head.
The recursive function reverseList reverses a singly linked list and returns the head of the reversed list. Initially, the function reverseList is called as reverseList(. Here, head is the head of the singly linked list before reversal and is of type ListElement. The table provides an explanation of the member variables of the class ListElement. ListElement-type variables store references to instances of the class ListElement.
Table Explanation of the member variables of the class ListElement
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The procedure insertAfter inserts an element into a singly linked list at the position after an existing element with a specific data value. The argument targetData is a string type data that represents the value of the existing element in the list, after which a new element is to be inserted. If no corresponding element exists, no action is taken. The argument newData is also a string type data represents the value of the new element to be inserted.
The class Element represents each element of the linked list. The figure explains the constructor and the member variables of the class Element. Element-type variables store references to instances of the class Element. A reference to the first element in the list is pre-stored in the global variable head.
Figure Class Element
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The procedure deleteLast removes an element at the end of a doubly linked list. Each element of the doubly linked list is represented by the class ListElement. The table shows the description of the class ListElement. The ListElement-type variable holds a reference to an instance of the class ListElement. The global variable listHead holds a reference to the head element of the doubly linked list. Remember that each element in the doubly linked list has a reference to its previous element and its next element. Here, if the list is empty, listHead is set to undefined.
The procedure handles three main cases: if the list is empty, it outputs "empty." If the list contains only one element, it becomes empty after deletion. If multiple elements are present, it only removes the last element.
Table Class ListElement
Answer group
From the answer group below, select the correct answer to be inserted into blank in the program.
The procedure addNode adds a node to a singly-linked list at the position specified by the argument pos. The argument pos is a positive integer that is equal to or less than the (number of nodes + 1) in the list. The position at the top of the list is 1.
The class ListNode represents a node in a singly-linked list. The table summarizes an explanation of the member variables of the class ListNode. ListNode-type variables store references to instances of the class ListNode. A reference to the first node in the list is pre-stored in the global variable listHead.
Table Explanation of the member variables of the class ListNode
Answer group
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The procedure Insert inserts an integer number given by the argument after the last element of the linear circular linked list. Each element of the linear circular linked list is represented by the class ListElement. The figure shows the description of the class ListElement. The ListElement-type variable holds a reference to an instance of the class ListElement. The global variable listHead holds a reference to the head element of the linear circular linked list. Remember that in the circular linked list the last element points to the listHead. Here, if the list is empty, listHead is set to undefined.
Figure Class ListElement
Answer group