IPA FE Subject B sample Question 10
A question IPA Japan published in 基本情報技術者試験 科目B サンプル問題 問10, translated into English. ITPEC is based on the same exams.
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
Explanation
prev.next.next — to unlink the element at pos, the element before it must point to the element after it.
The loop runs i from 2 to pos − 1, so prev stops on element pos − 1, the one just before the element to delete. That element is prev., and the one after it is prev.. Assigning it to prev. skips the deleted element, and nothing else needs to change. When pos is the last position, prev. is undefined, and prev becomes the new tail.
Example: for A → B → C → D, delNode( runs the loop once, so prev is B; B's next becomes D, giving A → B → D.
Answer (f)
Why not others:
- •(e) — assigns
prev.its own value, so nothing is deletednext - •(a) and (d) — point an element back at the head or at itself: for
pos2 the head's next becomes the head, a loop - •(b) and (c) — fixed positions counted from the head: (c) is right only when
posis 2 (thenprevislistHead), (b) deletes nothing atpos2 and forms a loop frompos3
Key rule: To delete from a singly linked list, stop on the predecessor and set its next to its next.; the head has no predecessor, so it is handled separately.
Linked List · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.