Skip to content

Linked list

storing

Level 1 of 3

Also called Sorted singly linked list.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from linked list's own run on the numbers shown.

choose one

question 1

An empty chain kept in order puts in 30, 12 and 45, takes 12 out, then puts in 20. What does the chain hold then, from the start?

the answer, and why

the answer is B

After 12 leaves, the chain holds 30, 45. 30 is bigger than 20, so the first look stops the walk and 20 goes in at the start.

  1. step 10 of 12

    30145

    30 is bigger than 20, so 20 goes just before it.

    looks so far: 1

  2. step 11 of 12

    2030145

    Link 20 in at the start of the chain.

    looks so far: 1

  • the start, holding no number
  • a look, numbered in turn
  • looked at now
  • just linked in, with its new pointers
Operation 5 puts 20 in, after operations 1 to 4 put in 30, 12 and 45 and took 12 out. 30 is bigger than 20, so 20 is linked in at the start.

if you chose another

  • That keeps 12, but the fourth operation takes 12 out.
  • That puts 20 at the end, but a put stops at the first bigger number, 30.
  • That loses 30. Taking 12 out links the start to 30, which stays.

new numbers

question 2

Starting from an empty chain kept in order, you put in 24, 9, 31 and 17, in that order. How many looks do the four puts make in all?

the answer, and why

the answer is A

24 finds the chain empty, 0 looks. 9 stops at 24, 1. 31 passes 9 and 24, 2. 17 passes 9 and stops at 24, 2. That is 5.

  1. step 8 of 11

    912431

    9 is not bigger than 17, so walk on past it.

    looks so far: 1

  2. step 9 of 11

    9124231

    24 is bigger than 17, so 17 goes just before it.

    looks so far: 2

  3. step 10 of 11

    911724231

    Link 17 in just before 24, and point it at 24.

    looks so far: 2

  • the start, holding no number
  • a look, numbered in turn
  • looked at now
  • looked at, and walked past
  • just linked in, with its new pointers
Operation 4 puts 17 in, after operations 1 to 3 put in 24, 9 and 31. It passes 9, stops at 24 and links 17 in between, 2 looks.

if you chose another

  • That counts every place each time, as smallest first would, 0 + 1 + 2 + 3.
  • That misses a look. 31 passes both 9 and 24 to reach the end.
  • That counts 1 look a put after the first, as biggest first would.

new numbers

question 3

Which order of putting 14, 26, 38 and 51 into an empty chain costs the fewest looks?

the answer, and why

the answer is A

Biggest first, each put after the first stops at its first look. 0 + 1 + 1 + 1 is 3.

  1. step 7 of 9

    2613851

    26 is bigger than 14, so 14 goes just before it.

    looks so far: 1

  2. step 8 of 9

    142613851

    Link 14 in at the start of the chain.

    looks so far: 1

  • the start, holding no number
  • a look, numbered in turn
  • looked at now
  • just linked in, with its new pointers
Biggest first, operation 4 puts 14 in, after 51, 38 and 26 went in. 26 is bigger than 14, so one look, and 14 goes in at the start.

if you chose another

  • That costs 5. 38 passes 14 and 26 before it stops at 51.
  • Smallest first is the dearest order, 0 + 1 + 2 + 3, which is 6.
  • That costs 5. 51 passes both numbers before it to reach the end.

new numbers