April
1999 QUESTION 2 Total Marks: 20 Marks |
Click here to access other
questions
Click to access
|
(a) | Identify and explain the difference between a stack
and a queue.
|
[2] |
(b) | Assume the following definition of a queue data
type: TYPE QueueType = RECORD Here, Front points to the first element in the queue, Rear points to the last element added to the queue, and the Next field of a node points from the older element in the queue to the next newer element in the queue. Furthermore, you can assume the existence of the following functions and procedures:
|
|
(i) Write a procedure popQueue - with the prototype given below -
which will remove and return the oldest element in a queue. You should call the
procedure Error if there is not an item in the queue to be popped. PROCEDURE popQueue(VAR queue : QueueType;
|
[4] | |
(ii) Write a procedure addQueue - with the prototype given below - which
will add an element to a queue. PROCEDURE
addQueue(Var queue: QueueType;
|
[4] | |
(iii) Write a procedure NpopQueue - with the prototype given below -
which will attempt to remove N items from a queue, and return the N'th item. When the
procedure returns, the parameter N should hold the value of the number of remaining items
to be popped. PROCEDURE NpopQueue(VAR queue : QueueType; VAR N:Integer;VAR datum : Integer);
|
[5] | |
(iv) Write a recursive procedure reverseQueue - with the prototype given below -
which will reverse the queue queue. PROCEDURE reverseQueue(VAR queue : queueType); |
[5] |