Linked List
9 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
From the answer group below, select the correct answer to be inserted into blank in the program.
The procedure delNode deletes the element at the position specified by the argument pos from a singly linked list. The argument pos is a positive integer that is equal to or less than the number of elements in the list. The position at the head of the list is 1.
The class ListElement represents an element of a singly linked list. The table shows an explanation of the member variables of the class ListElement. ListElement-type variables store references to instances of the class ListElement. A reference to the head element of the list is pre-stored in the global variable listHead.
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 append adds the character given as the argument to a singly linked list. Each element of the singly linked list is represented by the class ListElement. The figure shows the explanation of the class ListElement. ListElement-type variables store references to instances of the class ListElement. The global variable listHead stores a reference to the head element of the singly linked list. When the list is empty, listHead is undefined.
Figure Explanation 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. Here, the array index starts at 1.
A singly linked list is represented by two arrays, the array dataList and the array pointerList. dataList stores the values of the elements of the list, and pointerList stores the index of the element of dataList that corresponds to the next element of the list. The head of the singly linked list is the pair of dataList[1] and pointerList[1]. The element of pointerList that corresponds to the tail of the singly linked list is undefined. The elements of dataList that do not store values of elements of the singly linked list, and the corresponding elements of pointerList, are undefined.
The contents of dataList and pointerList handled by the program are shown in Figure 1. The index of the element next to the head is 3, stored in pointerList[1], and its value is 20, stored in dataList[3]. The index of the element after that is 2, stored in pointerList[3], and its value is 30, stored in dataList[2].
Figure 1 Contents of dataList and pointerList
The function orderList returns an array that stores the values of the singly linked list represented by dataList and pointerList in Figure 1, in order, following the singly linked list from its head. The array returned by the function orderList is shown in Figure 2.
Figure 2 Array returned by the function orderList
Answer group