Показать сообщение отдельно
  #2  
Старый 11.04.2011, 15:04
Аватар для NumLock
NumLock NumLock вне форума
Let Me Show You
 
Регистрация: 30.04.2010
Адрес: Северодвинск
Сообщения: 5,426
Версия Delphi: 7, XE5
Репутация: 59586
По умолчанию

реализация:

Код:
unit UnitQueue;

interface

type
  PElement = ^TElement;
  TElement = record
    Value: Integer;
    Parent: PElement;
  end;

  TQueue = class(TObject)
  private
    Top: PElement;
  public
    constructor Create;
    destructor Destroy; override;
    function GetValue: Integer;
    function IsEmpty: Boolean;
    function Pop: Integer;
    procedure Push(AValue: Integer);
  end;

implementation

constructor TQueue.Create;
begin
  Top:=nil;
end;

destructor TQueue.Destroy;
begin
  while not IsEmpty do Pop;
  inherited Destroy;
end;

function TQueue.GetValue: Integer;
begin
  Result:=Top^.Value;
end;

function TQueue.IsEmpty: Boolean;
begin
  Result:=Top=nil;
end;

function TQueue.Pop: Integer;
var
  Now: PElement;
begin
  Now:=Top;
  Result:=Now^.Value;
  Top:=Now^.Parent;
  Dispose(Now);
end;

procedure TQueue.Push(AValue: Integer);
var
  Now: PElement;
begin
  New(Now);
  Now^.Value:=AValue;
  Now^.Parent:=Top;
  Top:=Now;
end;

end.

использование:

Код:
FQueue: TQueue;

begin
  FQueue:=TQueue.Create;
  FQueue.Push(0);
  FQueue.Push(100);
  FQueue.Push(255);
  ListBox1.Items.Add(IntToStr(FQueue.GetValue));
  ListBox1.Items.Add(IntToStr(FQueue.GetValue));
  ListBox1.Items.Add(IntToStr(FQueue.GetValue));
  ListBox1.Items.Add(IntToStr(FQueue.Pop));
  ListBox1.Items.Add(IntToStr(FQueue.Pop));
  ListBox1.Items.Add(IntToStr(FQueue.Pop));
  FQueue.Free;
end;
__________________
Пишу программы за еду.
__________________
Ответить с цитированием