程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi 2009 泛型容器單元(Generics.Collections)[3]: TStack

Delphi 2009 泛型容器單元(Generics.Collections)[3]: TStack

編輯:Delphi

 TQueue 和 TStack, 一個是隊列列表, 一個是堆棧列表; 一個是先進先出, 一個是先進後出.

  TStack 主要有三個方法、一個屬性:

  Push(壓棧)、Pop(出棧)、Peek(查看下一個要出棧的元素);

  Count(元素總數).

  本例效果圖:

Delphi 2009 泛型容器單元(Generics.Collections)[3]: TStack

  代碼文件:unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;
type
 TForm1 = class(TForm)
  Memo1: TMemo;
  Button1: TButton;
  Button2: TButton;
  Button3: TButton;
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure Button3Click(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
uses Generics.Collections; {Delphi 2009 新增的泛型容器單元}
type
 TRec = record
  Name: string;
  Age: Word;
 end;
var
 Stack: TStack<TRec>; {定義一個泛型 TStack 類, 這指定了要用於上面定義的 TRec 記錄}
{建立}
procedure TForm1.FormCreate(Sender: TObject);
begin
 Stack := TStack<TRec>.Create;
 Memo1.Clear;
 Button1.Caption := Button1.Caption + ' 壓棧';
 Button2.Caption := Button2.Caption + ' 出棧';
 Button3.Caption := Button3.Caption + ' 下一個出棧的...';
end;
{釋放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
 Stack.Free;
end;
{壓棧: Push}
procedure TForm1.Button1Click(Sender: TObject);
var
 rec: TRec;
begin
 rec.Name := StringOfChar(Char(65 + Random(26)), 3);
 rec.Age := Random(150);
 Stack.Push(rec);
 Text := Format('當前隊列成員總數: %d', [Stack.Count]);
 {讓 Memo1 配合顯示}
 Memo1.Lines.Add(Format('%s, %d', [rec.Name, rec.Age]));
end;
{出棧: Pop}
procedure TForm1.Button2Click(Sender: TObject);
var
 rec: TRec;
begin
 if Stack.Count = 0 then Exit;
 rec := Stack.Pop;
 ShowMessage(Format('%s, %d', [rec.Name, rec.Age]));
 Text := Format('當前隊列成員總數: %d', [Stack.Count]);
 {讓 Memo1 配合顯示}
 Memo1.Lines.Delete(Memo1.Lines.Count - 1);
end;
{下一個出列的的元素: Peek}
procedure TForm1.Button3Click(Sender: TObject);
var
 rec: TRec;
begin
 if Stack.Count = 0 then Exit;
 rec := Stack.Peek;
 ShowMessage(Format('%s, %d', [rec.Name, rec.Age]));
end;
end.
窗體文件:object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClIEntHeight = 147
 ClIEntWidth = 284
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 Position = poDesktopCenter
 OnCreate = FormCreate
 OnDestroy = FormDestroy
 PixelsPerInch = 96
 TextHeight = 13
 object Memo1: TMemo
  Left = 0
  Top = 0
  Width = 121
  Height = 147
  Align = alLeft
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'CourIEr New'
  Font.Style = []
  Lines.Strings = (
   'Memo1')
  ParentFont = False
  ScrollBars = ssBoth
  TabOrder = 0
 end
 object Button1: TButton
  Left = 127
  Top = 21
  Width = 146
  Height = 25
  Caption = 'Button1'
  TabOrder = 1
  OnClick = Button1Click
 end
 object Button2: TButton
  Left = 127
  Top = 61
  Width = 146
  Height = 25
  Caption = 'Button2'
  TabOrder = 2
  OnClick = Button2Click
 end
 object Button3: TButton
  Left = 127
  Top = 99
  Width = 146
  Height = 25
  Caption = 'Button3'
  TabOrder = 3
  OnClick = Button3Click
 end
end


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved