Tqueue and Tstack, one is the queue list, one is the stack list; One is advanced first out, one is advanced and then out.
Tqueue has three main methods and one property:
Enqueue (into Row), Dequeue (dequeue), Peek (see the next element to be out);
Count (total number of elements).
This example:
Code files:
UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTForm1 =class(Tform) Memo1:tmemo; Button1:tbutton; Button2:tbutton; Button3:tbutton;procedureFormcreate (Sender:tobject);procedureFormdestroy (Sender:tobject);procedureButton1Click (Sender:tobject);procedureButton2click (Sender:tobject);procedureButton3click (Sender:tobject);End;varForm1:tform1;Implementation{$R *.DFM}usesGenerics.collections;{Delphi 2009 New Generic Container unit}typeTRec =RecordName:string; Age:word;End;varqueue:tqueue<trec>;{defines a generic Tqueue class, which specifies the TREC record to use for the definition above}{Build}procedureTform1.formcreate (Sender:tobject);beginQueue: = Tqueue<trec>. Create; Memo1.clear; Button1.caption: = Button1.caption +' into row '; Button2.caption: = Button2.caption +' dequeue '; Button3.caption: = Button3.caption +' next dequeue ... ';End;{Release}procedureTform1.formdestroy (Sender:tobject);beginQueue.free;End;{into row: Enqueue}procedureTform1.button1click (Sender:tobject);varRec:trec;beginRec. Name: = Stringofchar (Char ( $+ Random ( -)),3); Rec. Age: = Random ( Max); Queue.enqueue (REC); Text: = Format (' Total number of current queue members:%d ', [Queue.count]);{let Memo1 mate display}MEMO1.LINES.ADD (Format ('%s,%d ', [Rec. Name, rec. Age]));End;{dequeue: Dequeue}procedureTform1.button2click (Sender:tobject);varRec:trec;beginifQueue.count =0 ThenExit; Rec: = Queue.dequeue; ShowMessage (Format ('%s,%d ', [Rec. Name, rec. Age])); Text: = Format (' Total number of current queue members:%d ', [Queue.count]);{let Memo1 mate display}Memo1.Lines.Delete (0);End;{Next dequeue element: Peek}procedureTform1.button3click (Sender:tobject);varRec:trec;beginifQueue.count =0 ThenExit; Rec: = Queue.peek; ShowMessage (Format ('%s,%d ', [Rec. Name, rec. Age]));End;End.
Form file:
ObjectForm1:tform1 left =0Top =0Caption =' Form1 'ClientHeight =147ClientWidth =284Color = Clbtnface Font.charset = Default_charset Font.Color = Clwindowtext Font.height =- OneFont.Name =' Tahoma 'Font.style = [] Oldcreateorder = False Position = Podesktopcenter OnCreate = formcreate OnDestroy = Formdestroy Pixel Sperinch = theTextHeight = -ObjectMemo1:tmemo left =0Top =0Width =121Height =147Align = Alleft Font.charset = Default_charset Font.Color = Clwindowtext Font.height =- -Font.Name =' Courier New 'Font.style = [] Lines.strings = (' Memo1 ') Parentfont = False ScrollBars = Ssboth TabOrder =0Explicitheight =201EndObjectButton1:tbutton left =127Top = +Width =146Height = -Caption =' Button1 'TabOrder =1OnClick = Button1ClickEndObjectButton2:tbutton left =127Top = AWidth =146Height = -Caption =' Button2 'TabOrder =2OnClick = Button2clickEndObjectButton3:tbutton left =127Top = AboutWidth =146Height = -Caption =' Button3 'TabOrder =3OnClick = Button3clickEndEnd
Generic container Unit (generics.collections) [2]: tqueue<t> Queue list