Practice
Insert at the Front of a Linked List
2023/Oct/Nov·Variant 2
HARDLinked Lists
A singly linked list and a free list share the same pair of arrays, ListData and NextPointer (a classic technique for simulating pointers with arrays). StartPointer is the head of the list; FreePointer is the head of the chain of unused slots.
Write procedure InsertFront(Value) that:
- takes the next available node from the free list (advancing
FreePointer) - stores
Valuein it and links it in front of the current list (updatingStartPointer) - outputs
"List full"instead, without changing anything, if there is no free node (FreePointer = -1)
The program starts with the list Bob → Amy already stored, and a free chain of 3 unused slots. It then performs the given number of insertions and prints the final list.
Input: Number of names to insert, then each name.
Output: The final list, one name per line (plus "List full" for any insertion that could not be completed).
Example:
Input: 1
Zoe
Output: Zoe
Bob
Amy
Premium is coming soon. All grading features are currently unlocked.
Sample Test Cases
Test 1: Three insertions in a row
Inputs: 3, Cy, Dee, Eli
Expected: Eli
Dee
Cy
Bob
Amy
Test 2: Single insertion
Inputs: 1, Zoe
Expected: Zoe
Bob
Amy