We are now ready to develop a procedure to add a fan to the end of
the queue. Clearly, there are two different situations: an empty queue
and a non-empty queue. Although we only need tail to
extend the queue, we need head to determine whether the
queue is empty. So here is the header:
PROC add fan = (REF REF REF QUEUE head,tail,
REF FAN fan)VOID:
Firstly, we need to test whether the queue is empty:
IF head IS nilq
Remember that the mode of head is REF REF REF
QUEUE, so in the identity relation
head is dereferenced twice.
Secondly, if the queue is empty, we assign an
anonymous REF QUEUE to the name
head refers to and assign (fan,nilq) to the
REF QUEUE:
THEN REF REF QUEUE(head):=
LOC QUEUE:=(fan,nilq)
but this will not work because the scope of the LOC
QUEUE is limited to the routine denotation. We must use a
global generator:
THEN REF REF QUEUE(head):=
HEAP QUEUE:=(fan,nilq)
Then we have to ensure that tail refers to the tail
of the queue:
tail:=next OF head
If the queue is not empty, we assign an anonymous REF
QUEUE to the name that tail points to:
ELSE REF REF QUEUE(tail):=
HEAP QUEUE:=(fan,nilq)
and make tail refer to the new tail:
tail:=next OF tail
Here is the complete procedure:
PROC add fan = (REF REF REF QUEUE head,tail,
REF FAN fan)VOID:
IF head IS nilq
THEN #the queue is empty#
REF REF QUEUE(head):=
HEAP QUEUE:=(fan,nilq);
tail:=next OF head
ELSE
REF REF QUEUE(tail):=
HEAP QUEUE:=(fan,nilq);
tail:=next OF tail
FI #add fan#
add fan could be
optimised. Rewrite the body of add
fan so that the overall structure is
tail:=next OF (REF REF QUEUE
CO IF ... FI plus two assignments CO
)
Ans
Iain and Fiona and increment the ticket
numbers by 1. Compile and run the program to check that there are no
errors (no output will be produced). Ans
Sian Mountbatten 2012-01-19