Delphi队列和栈

2021-04-02 02:25

阅读:692

标签:lse   数据   forms   vat   tab   creat   obj   charset   integer   

----------------D7

 一般把栈叫堆栈;数据结构上的堆和栈是两个不同的东西;

------------------

--------------Unit

{Queue(队列),先进先出;Stack(堆栈),后进先出}
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Contnrs ; //,ExtCtrls

type
PMyrecord = ^TMyrecord;
TMyrecord = record
RCode: String;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Myqueue:TQueue;
Mystack:TStack;
PRe: PMyrecord;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
try
for i := 0 to 9 do
begin
New(PRe);
PRe.RCode:= IntToStr(i);
Myqueue.Push(PRe);
end;
while Myqueue.Count > 0 do
begin
PRe:= Myqueue.Pop;
memo1.Lines.Add(PRe.RCode);
Dispose(PRe);
end;
except

end;
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(Myqueue);
FreeAndNil(Mystack);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Myqueue:= TQueue.Create;
Mystack:= TStack.Create;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
begin
try
for i := 0 to 9 do
begin
New(PRe);
PRe.RCode:= IntToStr(i);
Mystack.Push(PRe);
end;
while Mystack.Count > 0 do
begin
PRe:=Mystack.Pop;
Memo1.Lines.Add(PRe.RCode);
Dispose(PRe);
end;
except
end;
end;


end.

---------------Unit-

 

----------------Form

object Form1: TForm1
Left = 840
Top = 634
Width = 305
Height = 311
Caption = ‘Form1‘
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘MS Sans Serif‘
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 192
Top = 24
Width = 75
Height = 25
Caption = ‘B1_queue‘
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 0
Top = 0
Width = 185
Height = 273
Align = alLeft
ImeName = ‘中文(简体) - 搜狗拼音输入法‘
ScrollBars = ssBoth
TabOrder = 1
end
object Button2: TButton
Left = 192
Top = 176
Width = 75
Height = 25
Caption = ‘B2_stack‘
TabOrder = 2
OnClick = Button2Click
end
end

------------------Form---

Delphi队列和栈

标签:lse   数据   forms   vat   tab   creat   obj   charset   integer   

原文地址:https://www.cnblogs.com/dmqhjp/p/14597259.html


评论


亲,登录后才可以留言!