以Delphi 實作Queue 類別,含範例 |
|
ddy
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:262 回覆:2105 積分:1169 註冊:2002-07-13 發送簡訊給我 |
據 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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |