線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4007
推到 Plurk!
推到 Facebook!

以Delphi 實作Queue 類別,含範例

 
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-03-16 00:01:58 IP:61.59.xxx.xxx 未訂閱
據 superlevin 版主告之,有個Queue 的需求,我便實作了一個TQueue  在此野人獻曝一番,僅用到簡單的動態陣列,實現環型佇列 若程式有誤尚請資料結構之高手多多指教 有興趣研究的朋友,也可以改成串列佇列或是以指標來實現或是加入其它檢查
    unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
//--------------------宣告TQueue -----------------------------------
type
  TQueue = class
  private
    FBuffer :array of Variant;  //以陣列當Buffer
    FBufferSize :Integer;  //記錄buffer大小
    FPosition :Integer;   //指向目前欲取出的項目位置
    FIndex    :Integer;   //目前資料的位置
  public
    procedure Push(iData:Variant);
    Function Pop():Variant ;
    procedure SetBuffer(iSize:Integer); //設定buffer 大小
    property BufferSize :Integer read FBufferSize Write SetBuffer; //屬性
  end;        var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TQueue.Push(iData:Variant);
begin
    FBuffer[FIndex]:=iData; //寫入資料
    Inc(FIndex);  //index 下移
    FIndex :=FIndex mod FBufferSize ; //若已到底則重頭開始
end;    function TQueue.Pop():Variant ;
begin
    Result:=FBuffer[FPosition]; //讀出資料
    Inc(FPosition); //Position 下移
    FPosition :=FPosition mod FBufferSize ;   //若已到底則重頭開始
end;    procedure TQueue.SetBuffer(iSize:Integer);
begin
    SetLength (FBuffer,iSize);  //Buffer 初始
    FIndex :=0;
    FPosition :=0;
    FBufferSize:=iSize;  //傳回buffer size
end;
procedure TForm1.Button1Click(Sender: TObject);
var
    q :TQueue ;
begin
    q:=TQueue.Create ;   //建立TQueue 物件
    q.SetBuffer (5);
    q.Push ('a');
    q.Push('b');
    q.Push('c');
    q.Push(4);
    q.Push(5);
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;
    ListBox1.Items.Add (q.Pop )  ;        self.Caption :=IntToStr( q.BufferSize);   //取得Buffer 大小
    q.Free ; //釋放TQueue
end;    end.    
========以下程式碼同上,供直接複製使用========================= unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; //--------------------宣告TQueue ----------------------------------- type TQueue = class private FBuffer :array of Variant; //以陣列當Buffer FBufferSize :Integer; //記錄buffer大小 FPosition :Integer; //指向目前欲取出的項目位置 FIndex :Integer; //目前資料的位置 public procedure Push(iData:Variant); Function Pop():Variant ; procedure SetBuffer(iSize:Integer); //設定buffer 大小 property BufferSize :Integer read FBufferSize Write SetBuffer; //屬性 end; var Form1: TForm1; implementation {$R *.dfm} procedure TQueue.Push(iData:Variant); begin FBuffer[FIndex]:=iData; //寫入資料 Inc(FIndex); //index 下移 FIndex :=FIndex mod FBufferSize ; //若已到底則重頭開始 end; function TQueue.Pop():Variant ; begin Result:=FBuffer[FPosition]; //讀出資料 Inc(FPosition); //Position 下移 FPosition :=FPosition mod FBufferSize ; //若已到底則重頭開始 end; procedure TQueue.SetBuffer(iSize:Integer); begin SetLength (FBuffer,iSize); //Buffer 初始 FIndex :=0; FPosition :=0; FBufferSize:=iSize; //傳回buffer size end; procedure TForm1.Button1Click(Sender: TObject); var q :TQueue ; begin q:=TQueue.Create ; //建立TQueue 物件 q.SetBuffer (5); q.Push ('a'); q.Push('b'); q.Push('c'); q.Push(4); q.Push(5); ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; ListBox1.Items.Add (q.Pop ) ; self.Caption :=IntToStr( q.BufferSize); //取得Buffer 大小 q.Free ; //釋放TQueue end; end.
superlevin
高階會員


發表:181
回覆:313
積分:180
註冊:2003-01-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-03-17 06:53:48 IP:211.76.xxx.xxx 未訂閱
真的很感謝DDY大哥唷~因為Levin讀資管,沒學過實際設計資料結構的課程~所以這陣子問了DDY大哥許多問題真不好意思~還得麻煩他呢!不過~
------
林壽山
網站: http://superlevin.ifengyuan.tw
mail: superlevin@gmail.com
系統時間:2024-04-25 23:42:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!