program SeqQueue;

const
  max = 10;  {Maximum size of Queue}

type
  itemtype = integer; {Contents of Queue}

  queue = record       {Queue Structure}
    items: array [1..max] of itemtype;
    front: integer;
  end;


{*****************************************
Procedure to Initialise a Queue
Pre: Queue not initialised
Post:Queue initialised with front and
     back at the same position.
******************************************}
procedure init(var q:queue);
  begin
    q.front:=1;
  end;


{*****************************************
Function to return True/False if queue is full
Pre: queue initialised
Post:queue unchanged
******************************************}
function isFull(q:queue): boolean;
begin
isFull:=q.front=max+1;
end;

{*****************************************
Function to return True/False if queue is empty
Pre: Queue initialised
Post:Queue unchanged
******************************************}
function isEmpty(q:queue): boolean;
begin
isEmpty:=q.front=1;
end;


{*****************************************
Function to return number of items in Queue
Pre: Queue initialised
Post:Queue unchanged
******************************************}
function itemNo(q:queue): integer;
begin
itemNo:=q.front-1;
end;


{*****************************************
Procedure to Insert an item at back of Queue
Pre: Queue initialised and not full
Post:Queue has item inserted at back
******************************************}
procedure join(var q:queue;i:itemtype);
  var c:integer;
  begin
    if (isFull(q))
     then writeln('Sorry - Queue is Full')
     else
       begin
         if q.front>1
            then
                for c:=q.front downto 2 do
                    q.items[c]:=q.items[c-1];
         q.items[1]:=i;
         q.front:=q.front+1;
       end;
  end;


{*****************************************
Procedure to Remove an item from front of Queue
Pre: Queue initialised and not empty
Post:Queue has item removed from top
******************************************}
procedure leave(var q:queue;var i:itemtype);
  begin
    if (isEmpty(q))
     then writeln('Sorry - Queue is Empty')
     else
       begin
         q.front:=q.front-1;
         i:=q.items[q.front];
       end;
  end;


{*****************************************
               MAIN PROGRAM
******************************************}
Var
  TestQueue:queue;
  item:itemtype;

BEGIN
  init(TestQueue);
  join(TestQueue,21);
  join(TestQueue,24);
  join(TestQueue,28);
  writeln('Number of items in Queue: ',itemNo(TestQueue));
  leave(TestQueue,item);
  writeln('The item at the front of the Queue was ',item);
  writeln('Number of items now in Queue: ',itemNo(TestQueue));
  join(TestQueue,1);
  join(TestQueue,2);
  join(TestQueue,3);
  join(TestQueue,4);
  join(TestQueue,5);
  join(TestQueue,6);
  join(TestQueue,7);
  join(TestQueue,8);
  join(TestQueue,9);
END.
