{############################################################################ # QUEUE IMPLEMENTATION (first-in first-out) # USING CIRCULAR ARRAY # # by DAVE HEYLIGER - AMUS STAFF # # NOTES: 1) The user must include a file called QUEUE.TYP that defines the # procedure PRINT and describes the elementtype of the Queue. This # file is the only file to modify when changing the elementtype. # 2) If the queue is to hold more that 50 elements, the constant 'max' # must be changed in QUEUE.TYP to one LESS than max! # 3) A file called QUEUE.EXT should be included in the driver program. # This file contains the external definitions of the func/proc's # below. # 4) Final linkage: QUEUE.PAS and your Driver Program. # # Last Update: 08/26/85 ############################################################################} MODULE QUEUEMOD; {+-- This module is the implementation of a Q adt. The following | operations are provided: | | queuemakenull - creates an empty Q | | queuenqueue - enqueues an element into Q | | queuedequeue - removes front element from Q | | queuefront - returns the fromt Q element | | queuempty - returns false iff Q emtpy | | queueprint - prints queue in readable form +-------------------------------------------------------------------} {$I queue.typ} procedure queuemakenull(var q: queue); {+--- on entry - true | on exit - q is initialized and empty +------------------------------------------} begin { queue_makenull } q.front := 0; q.rear := 0; q.status := empty; end; { queue_makenull } procedure queuenqueue(var q: queue; x: queuelement); {+--- on entry - q was previously initialized | on exit - x is enqueued in the rear of q +-----------------------------------------------} begin { queue_enqueue } if (q.front = q.rear) and (q.status = full) then writeln('Module:queue, procedure:queue_enqueue, Q full') else begin { enqueue element } q.space[q.rear] := x; q.rear := (q.rear+1) mod max; if q.rear = q.front then q.status := full; end; { enqueue element } end; { queue_enqueue } procedure queuedequeue(var q: queue); {+--- on entry - q is non empty | on exit - the fron element is removed from Q +---------------------------------------------------} begin { queue_dequeue } if (q.rear=q.front) and (q.status=empty) then writeln('Module:queue, procedure:queue_dequeue, Q empty') else begin { dequeue } q.front := (q.front+1) mod max; if q.front=q.rear then q.status := empty; end; { dequeue } end; { queue_dequeue } function queuefront(q: queue): queuelement; {+--- on entry - q is not empty | on exit - front element of Q is returned +----------------------------------------------} begin { queue_front } queuefront := q.space[q.front]; end; { queue_front } function queuempty(q: queue): boolean; {+--- on entry - q is been initialized previously | on exit - returns true iff Q is mepty false otherwise +------------------------------------------------------------} begin { queue_empty } queuempty := (q.rear=q.front) and (q.status = empty); end; { queue_empty } procedure queueprint(q: queue; var out: text); {+--- on entry - q has been initialized previously | on exit - contents of Q is printed from front to rear on file out +-----------------------------------------------------------------------} var i: integer; begin { queue_print } i := q.front; if not queuempty(q) then repeat { until all printed } print(output,q.space[i]); i := i + 1; until i mod max = q.rear; { until all printed } end; { queue_print } . .