Date | November 2020 | Marks available | 6 | Reference code | 20N.2.HL.TZ0.17 |
Level | HL | Paper | 2 | Time zone | no time zone |
Command term | Construct | Question number | 17 | Adapted from | N/A |
Question
A new parking service has been introduced: staff will park the car for the driver and then return it later when the driver wants to leave. This service will use a narrow parking area surrounded on three sides by walls, where any cars behind the required car must be reversed out. For example, car X00000011
was the first vehicle to be parked, so to remove it from the parking area, the other five cars, starting with X00000123
, will need to be removed. The cars are then returned starting with car X00000010
.
Figure 5: Cars parked in the parking area
The class Stack
, which contains the Vehicle
objects, has the following methods:
The method staffRemoveCar
will remove a vehicle with a specified registration plate from the parking area (you can assume the vehicle is in the parking area):
public static void staffRemoveCar(Stack stack, String reg){
Stack temp = new Stack();
//add code here
}
Construct the staffRemoveCar
method.
A stack can be implemented using an array or a singly linked list.
Compare the use of these two data structures for creating a stack.
Markscheme
Award [6 max].
Award [1] for looping around the main stack using isEmpty() method;
Award [1] for using "while" loop in both loops (note: not a "do while"!);
Award [1] for breaking out of outer loop when car is removed and other cars are replaced;
Award [1] for correctly removing cars before target car is found;
Award [2] for replacing cars in same order by popping off temporary stack;
Example 1:
public void staffRemoveCar(Stack stack, String reg) {
Stack temp = new Stack();
while (!stack.isEmpty()) {
Vehicle v = (Vehicle) stack.pop();
if (v.getRegistration().equals(reg)) {
// correct car is now removed
// return others
while (!temp.isEmpty()) {
stack.push(temp.pop());
}
break; // don't keep repeating
}
temp.push(v);
}
}
Example 2:
public static void staffRemoveCar(Stack<Vehicle> stack, String reg)
{ Stack<Vehicle> temp = new Stack<Vehicle>();
boolean found = false;
while (!found && !stack.isEmpty())
{ Vehicle curr = stack.pop();
found = curr.getRegistration().equals(reg);
if (!found) temp.push(curr);
}
while (!temp.isEmpty()) stack.push(temp.pop());
}
Award [4 max].
Award [1] for an initial valid comparison and [1] for a valid expansion.
Memory
Linked lists can deal with memory issues more efficiently than arrays;
Arrays are static so will have a maximum size and cannot grow beyond that;
Memory will be allocated for maximum size at instantiation so while it is not full memory is wasted Links are dynamic and can add/remove memory as required- no memory is wasted;
Implementation
No particular advantage is gained by the use of either structure;
As additions and removals (push and pop) work on one end of the stack, navigation speed (i.e. O1) is not very helpful; Linked lists can adjust the pointer at the end of the list (can use a tail pointer) so slow navigation is not an issue;
Errors
The use of arrays is more likely to lead to run-time errors;
As arrays can reach their maximum limit whilst linked lists are only limited in size by the specifications of the CPU;
Examiners report
There were many full marks gained for this algorithm with students making correct use of the various stack methods.
Most alluded to the memory issues regarding the two data structures, but too many dealt incorrectly with issues dealing with traversing the structures, which were largely irrelevant as access was only needed for the top of the stack.