Worked solution
(a) Step-by-step state of the linked list:
- After Enqueue('Alice', 2): Head -> [ 'Alice', 2, Next -> null ]
- After Enqueue('Bob', 4): Head -> [ 'Bob', 4, Next ] -> [ 'Alice', 2, Next -> null ] (Bob has higher priority)
- After Enqueue('Charlie', 2): Head -> [ 'Bob', 4, Next ] -> [ 'Alice', 2, Next ] -> [ 'Charlie', 2, Next -> null ] (Charlie has same priority as Alice, so he is placed after Alice due to first-come first-served)
- After Dequeue(): Removes the head node ('Bob', 4).
Final List State: Head -> [ 'Alice', 2, Next ] -> [ 'Charlie', 2, Next -> null ]
(b) Pseudocode:
```
newNode = new Node(newName, newPriority)
if head == null then
head = newNode
else if newPriority > head.Priority then
newNode.Next = head
head = newNode
else
current = head
while current.Next != null and current.Next.Priority >= newPriority do
current = current.Next
end while
newNode.Next = current.Next
current.Next = newNode
end if
```
(c) Advantage: Dynamic memory allocation means the linked list does not have a fixed size limit, preventing overflow issues. Disadvantage: Extra memory is consumed to store the pointer addresses ('Next') for each node, and traversing the linked list to find the insertion point takes O(N) time compared to direct indexing.
Marking scheme
Part (a): Award up to [5 marks] max.
- Award [1 mark] for showing Alice correctly enqueued at first.
- Award [1 mark] for showing Bob correctly inserted at the head (before Alice).
- Award [1 mark] for showing Charlie correctly placed after Alice (same priority, stable ordering).
- Award [1 mark] for showing Dequeue correctly removes Bob.
- Award [1 mark] for showing correct pointer transitions, Head pointer, and null pointer at the tail.
Part (b): Award up to [8 marks] max.
- Award [1 mark] for creating the new node correctly with fields set.
- Award [1 mark] for checking the empty list boundary condition (head == null).
- Award [1 mark] for checking the 'insert at head' condition (newPriority > head.Priority).
- Award [1 mark] for updating head pointers correctly when inserting at the head.
- Award [1 mark] for initializing a traversal pointer (current = head).
- Award [1 mark] for implementing a loop to traverse nodes with higher or equal priority.
- Award [1 mark] for checking current.Next != null to prevent null pointer exceptions.
- Award [1 mark] for correctly adjusting Next pointers of current and newNode upon insertion.
Part (c): Award up to [2 marks] max.
- Award [1 mark] for a valid advantage (e.g., dynamic size, no resizing overhead).
- Award [1 mark] for a valid disadvantage (e.g., pointer memory overhead, lack of random access, sequential search required for insertion).