Witam. Napisałem sobie kolejke w tablicy statycznej. Program się odpala, jednak przy odpaleniu inita wywala błąd, ze wskazaniem w delpi na linijke Front:=1; Wiecie w czym może być problem ?
unit Unit2;
interface
uses Dialogs;
const
MaxLen = 20;
type
TElement = String[10];
TCQueue = class(TObject)
private
Queue: Array[1..MaxLen] of TElement;
Front: Longint; // Indeks elementu czołowego
Rear : Longint; // Indeks ostatniego elementu
Count: Longint; //liczba elementów
function AddOne(i: Longint): Longint;
public
procedure Init;
procedure EnQueue(value : TElement);
procedure DeQueue;
procedure Makenull;
function FrontElem: TElement;
function Empty : Boolean;
function ToText : String;
end;
implementation
procedure TCQueue.Init;
begin
inherited Create;
Front:=1;
Rear:= MaxLen;
Count:=1;
end;
function TCQueue.AddOne(i: Longint): Longint;
begin
Result := (i mod MaxLen)+1;
end;
function TCQueue.Empty : Boolean;
begin
Empty := (AddOne(Rear) = Front);
end;
procedure TCQueue.EnQueue(value : TElement);
begin
if ( AddOne(AddOne(Rear)) = Front) // Kolejka pełna
then
ShowMessage ('Kolejka pelna !')
else
begin
Rear := AddOne(Rear);
Queue[Rear] := value;
Inc(Count);
end
end;
procedure TCQueue.DeQueue;
begin
if (Empty) then
ShowMessage('Kolejka pusta !')
else
begin
Front := AddOne(Front);
Dec(Count);
end
end;
procedure TCQueue.Makenull;
begin
inherited Destroy;
end;
Function TCQueue.FrontElem : TElement;
Begin
If Empty Then ShowMessage('kolejka pusta')
Else RESULT := Queue[front];
End;
function TCQueue.ToText : String;
const
CR = #13#10;
var
i : Longint;
begin
Result := '';
for i := 0 to Count-1 do
Result := Result +
Queue[(Front + i) mod MaxLen] + CR;
end;
end.
A tutaj obsługa buttonów:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Unit2,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
GroupBox1: TGroupBox;
BInit: TButton;
BMakeNull: TButton;
BEnqueue: TButton;
BDeQueue: TButton;
BFrontElem: TButton;
procedure Print(S: TCQueue);
procedure BInitClick(Sender: TObject);
procedure BMakeNullClick(Sender: TObject);
procedure BEnqueueClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var S : TCQueue;
{$R *.dfm}
procedure TForm1.BInitClick(Sender: TObject);
begin
S.Init;
end;
procedure TForm1.BMakeNullClick(Sender: TObject);
begin
S.Makenull;
end;
procedure TForm1.Print(S:TCQueue);
begin
Memo1.Clear;
Memo1.Lines.Add(S.ToText);
end;
procedure TForm1.BEnqueueClick(Sender: TObject);
var x:String[10];
begin
x:=(InputBox('kolejka', 'Podaj element:', '1'));
S.Enqueue(x);
Print(s);
end;
end.