December
1998 QUESTION 3 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS
|
(a) | Define and explain the properties of a queue abstract data type. In particular, identify any error conditions that may be raised when manipulating a queue. | [3] |
Properties of a queue
one mark for each point, up to a maximum of three marks.
|
||
(b) | Identify and explain the differences between a dynamic and a static implementation of a queue. | [2] |
Differences between a
dynamic and static queue.
one mark for each point, up to a maximum of two marks.
|
||
(c) | This question is concerned with the
implementation of a doctors waiting room queuing system. On entry to the clinic, a patient
receives a patient-number that identifies their position within the queue. The
following data-type is used as a representation of the clinic's queue: TYPE Patient = RECORD NodeType = ^Node; Node = RECORD QueueType = RECORD where front points to the first element in the queue; rear pointers to the last element added to the queue; and the next field of a Node points from older elements in the queue to newer ones. i.e.,
Write a function MaxPatientId, that has the type shown below, that returns the largest patient identifier (i.e., the id field of the type Patient) within a queue. If the queue is empty, the function should return zero. FUNCTION MaxPatientId(queue: QueueType):Integer;
|
[4] |
A definition of
MaxPatientId follows: FUNCTION
MaxPatientId(queue:QueueType):Integer;
|
||
(d) | If the queue shown above were passed to the
procedure unknown with the string "John", then the state of the queue would be
changed. Re-draw the queue to show the effect of calling the procedure on the queue. PROCEDURE unknown(VAR queue: QueueType; name:
String[20]);
|
[2] |
A picture of the updated
queue:
|
||
(e) | The procedure unknown is not well-defined if an empty queue is passed as an argument. Rewrite the procedure so that it can take either an empty or non-empty queue. | [3] |
PROCEDURE unknown(VAR
queue: QueueType; newp: String[20]); VAR temp: NodeType; BEGIN New(temp); temp^.data.name := name; temp^.data.id := MaxPatientID(queue) + 1; temp^.next := Nil; IF queue.front = Nil THEN queue.front := temp; ELSE queue.rear^.next := temp; queue.rear := temp; END;
|
||
(f) | Write a function deQueue, with type shown below, that returns True and copies the patient information from the front of the queue into the
variable x if the queue if non-empty; and returns False otherwise. FUNCTION deQueue(queue:QueueType; VAR x: Patient):Boolean; |
[6] |
A definition of deQueue
follows: PROCEDURE
deQueue(queue: QueueType; VAR x:Patient):Boolean;
|