TQueueDemo

This demonstration of the TQueue class requires pointers. You should find it helpful to study our tutorial Lists, Stacks and Queues before attempting to understand the code.

program TQueueDemo;
  {$APPTYPE CONSOLE}
uses
  SysUtils, Contnrs;
const
  MAX = 3;
type
  PtrProg = ^TProg;
  TProg = record
    RaceNum : integer;
    Name : string[15];
  end;
var
  PtrFirst, PtrShow : PtrProg;
  Choice : integer;
  Queue : TQueue;
  Competitors : Array[1..MAX] of TProg;
  Found : Boolean;

procedure AppendRecord;
var
  PtrNew : PtrProg;
  Competitor, i, CurrentNum : integer;
begin
  for Competitor := 1 to MAX do
    begin
      repeat
        write('RaceNum (19, 23 or 35?) ');
        readln(CurrentNum);
        Found := False;
        i := 0;
        repeat
          inc (i);
          if Competitors[i].RaceNum = CurrentNum then
            begin
              new(PtrNew);
              Found := True;
              PtrNew^.RaceNum := CurrentNum;
              PtrNew^.Name := Competitors[i].Name;
              Queue.Push(PtrNew);
            end;
          until Found or (i = MAX);
          if Not Found then
            writeln('Not found');
      until Found;
    end;
end;


procedure DisplayRecord(RecPtr : PtrProg);
begin
  writeln(' Race number: ', RecPtr^.RaceNum,
           '   Name: ', RecPtr^.Name);
end;

procedure Init;
begin
  PtrFirst := nil;
  Competitors[1].RaceNum := 19;
  Competitors[1].Name := 'Brown';
  Competitors[2].RaceNum := 23;
  Competitors[2].Name := 'Steele';
  Competitors[3].RaceNum := 35;
  Competitors[3].Name := 'Finlay';
end;

begin
  Queue := TQueue.Create;
  Init;
  repeat
    writeln(#13#10'Please type the number of your choice.');
    writeln('1 - Enter results');
    writeln('2 - Dequeue records to view finishers in order');
    writeln('3 - Quit');
    readln(Choice);
    case choice of
      1 : AppendRecord;
      2 : begin
            writeln(#13#10'Competitors in order of finishing:'#13#10);
            while Queue.Count > 0 do
              begin
                PtrShow := Queue.Pop;
                DisplayRecord(PtrShow);
              end;
          end;
    end;
  until Choice = 3;
  Queue.Free;
end.
  
Programming - a skill for life!

Demonstrations of TStringList, TFileString, TQueue and TRegExp and drawing on a canvas