zoukankan      html  css  js  c++  java
  • Delphi队列和栈

    ----------------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---

  • 相关阅读:
    网络受限下,使用Nexus要解决的两个问题
    Nexus远程Maven仓库索引下载教程
    maven--私服的搭建(Nexus的使用)
    maven命令/依赖/聚合
    mybatis常用jdbcType数据类型
    Lombok 安装、入门
    jquery append 动态添加的元素事件on 不起作用的解决方案
    Maximum Sum on Even Positions
    哈密顿
    计算几何基础
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14597259.html
Copyright © 2011-2022 走看看