Linked List

Linked List

6 questions · Fundamental Engineering

Practice

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

Member variableTypeDescription
valintegerThe value of an element.
nextListElementReference to the instance that holds the next element in the list. If no next element exists, the status is undefined.
[Program]
○ ListElement: mergeTwoSortedList(ListElement: head1,
ListElement: head2)
ListElement: result
if (head1 is undefined)
return head2
elseif (head2 is undefined)
return head1
endif
if (A)
result ← head1
result.next ← mergeTwoSortedList(B, head2)
else
result ← head2
result.next ← mergeTwoSortedList(head1, C)
endif
return result

Answer group

OptionABC

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(head, undefined). 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

Member variableTypeDescription
valintegerThe value of the element.
nextListElementReference to the next element, if there is no next element, the status is undefined.
[Program]
○ ListElement: reverseList(ListElement: head, ListElement: elm)
ListElement: listHead
if (head.next is not undefined)
listHead ← reverseList(A, head)
else
listHead ← head
endif
head.next ← B
return listHead

Answer group

OptionAB

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.

ConstructorDescription
Element(string: data)Initialize the element with the data passed as an argument, which stored in the member variable data, with the member variable next set to undefined.
Member variableTypeDescription
datastringData associated with the element.
nextElementThis attribute represents the reference to the next element.

Figure Class Element

[Program]
global: Element: head // stores first element in the list
○ insertAfter(string: targetData, string: newData)
Element: x, y
x ← head
while(x is A)
if (x.data = targetData)
y ← Element(newData)
y.next ← B
x.next ← y
exit the while block
else
x ← x.next
endif
endwhile

Answer group

OptionAB

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

Member VariableTypeDescription
dataintegerThe value of an element.
nextListElementReference to the instance that holds the next element in the list.
prevListElementReference to the instance that holds the previous element in the list.
[Program]
global: ListElement: listHead /* A reference to the first element of
the list is stored. */
○ deleteLast()
ListElement: current
if (listHead is undefined)
output "empty"
else
current ← listHead
while (A is not undefined)
current ← current.next
endwhile
if (current.prev is not undefined) // multiple elements are present
B ← undefined
else // only one element is present
listHead ← undefined // empty list
endif
endif

Answer group

OptionAB

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

Member variableTypeDescription
valcharacterThe value of a node.
nextListNodeA reference for the next node.If no next node exists, the status is undefined.
[Program]
global: ListNode: listHead // stores the first node in the list
○ addNode(integer: pos, character: val)
ListNode: prev, newNode
integer: i
newNode ← ListNode()
newNode.val ← val
if (pos is equal to 1)
newNode.next ← listHead
listHead ← newNode
else
prev ← listHead
/* if pos is equal to 2, the following iteration process is not executed */
for (increase i from 2 to pos - 1 by 1)
prev ← prev.next
endfor
newNode.next ← prev.next
blank ← newNode
endif

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.

Member variableTypeDescription
valintegerThe value of an element.
nextListElementReference to the instance that holds the next element in the list.
ConstructorDescription
ListElement(integer: newItem)Initialize the member variable val with the argument newItem.

Figure Class ListElement

[Program]
global: ListElement: listHead ← undefined
○ Insert(integer: newItem)
ListElement: tmp, newNode
newNode ← ListElement(newItem)
if (listHead is undefined)
listHead ← newNode
listHead.next ← listHead
else
tmp ← listHead
while (tmp.next is not A)
tmp ← B
endwhile
tmp.next ← newNode
newNode.next ← listHead
endif

Answer group

OptionAB