Programming Basics (Pseudocode Tracing)

The following program inserts "F", in val[6], right after "C" (index 3) in the linked list A → C → E → D → B, linked by the array next, and then outputs the list from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What should replace 【a】 so that the output is A, C, F, E, D, B?

string array: val ← {"A", "B", "C", "D", "E", "F"}
integer array: next ← {3, 0, 5, 2, 4, 0}
【a】
integer: p ← 1
while (p ≠ 0)
  output val[p]
  p ← next[p]
endwhile
  1. Anext[3] ← 6, then next[6] ← next[3]
  2. Bnext[6] ← 3, then next[3] ← next[6]
  3. Cnext[6] ← next[3], then next[3] ← 6
Show answer and explanation

Answer: C. next[6] ← next[3], then next[3] ← 6

To insert, first make the new element F point to E, which C pointed to (next[3] = 5), and then make C point to F. next[6] ← next[3] gives next[6] = 5, and the following next[3] ← 6 links C → F → E. In the opposite order, next[6] ← next[3] runs after next[3] has become 6, so next[6] = 6: F points to itself and the output C, F, F, … never ends. When relinking a linked list, assign in an order that never loses the path to the following elements.

  • A. next[6] becomes 6, so F points to itself and the traversal never ends.
  • B. This gives next[6] = 3 and next[3] = 3, so C points to itself and the output A, C, C, … never ends.
  • C. Correct. F is linked to E first, then C is linked to F.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. The following for loop increases its control variable by 5. What is the value of c after it runs?
  2. In the following program, and does not evaluate its right side when the left side is false (short-circuit evaluation). cnt is a global variable. What is the value of cnt after the program runs?
  3. The following program searches the n-element array a for the value x and stores the index where it is found in i. Array indices start at 1. and and or do not evaluate their right side when the left side alone decides the result. Which condition in 【a】 reads a[n + 1], outside the array, when x is not in a?
  4. Real values are represented in IEEE 754 double precision (binary64), and each sum is rounded to the nearest representable value. What is the value of c after the following program runs?
  5. Class Counter gives each instance its own integer member variable count (0 when created), and its method inc() adds 1 to its own count. What is the value of x after the following program runs?