Question 1A singly linked list is stored in parallel arrays Data[0:3], Next[0:3]. Head=0; Data[0]=10,Next[0]=2; Data[2]=30,Next[2]=-1; slot1 is free. Insert20 in sorted order using slot1, showing assignments in safe order. Write pseudocode to search the resulting list for30, returning its array slot or-1, without assuming consecutive occupied slots. Separately insert10,6,14,8 into an empty binary search tree and state its inorder traversal.6 indicative marks
A singly linked list is stored in parallel arrays Data[0:3], Next[0:3]. Head=0; Data[0]=10,Next[0]=2; Data[2]=30,Next[2]=-1; slot1 is free. Insert20 in sorted order using slot1, showing assignments in safe order. Write pseudocode to search the resulting list for30, returning its array slot or-1, without assuming consecutive occupied slots. Separately insert10,6,14,8 into an empty binary search tree and state its inorder traversal.
Show indicative guidance
Worked guidance
Data[1] <- 20 Next[1] <- Next[0] Next[0] <- 1 DECLARE Current : INTEGER DECLARE Result : INTEGER Current <- Head Result <- -1 WHILE Current <> -1 IF Data[Current] = 30 THEN Result <- Current Current <- -1 ELSE Current <- Next[Current] ENDIF ENDWHILE OUTPUT Result- Save the successor into the new node first; setting Next[0]=1 before reading its old value would lose the intended successor and could make a self-loop. The occupied slots need not be consecutive: following links finds10 at0,20 at1,30 at2 and returns2. Missing targets eventually reach sentinel-1 without accessing Data[-1].
- For the separate BST, the comparison path for8 is10(left),6(right), giving nodes10 with left6/right14 and6 with right8. Inorder visits left subtree, root, right subtree and yields6,8,10,14. This is selected list implementation and tree insertion reasoning, not full linked-list/tree deletion coverage.
Self-marking points
View marking points
- Set Data[1]=20 and Next[1]=2 before changing the existing link.
- Set Next[0]=1, preserving Head0 and final chain0->1->2->-1.
- Search begins at Head and follows Next rather than incrementing a position through memory.
- Check current=-1 before accessing Data; search30 visits0,1,2 and returns2.
- BST places6 left of10,14 right of10,8 right of6.
- Inorder traversal is6,8,10,14; linked-list order is10,20,30.
Examiner tip
Indicative distinct points, not an official Cambridge mark allocation. State bounds, assumptions and intermediate states; practical teacher review remains required.