Delphi 2009 Generic Container Unit (generics.collections) [2]: tqueue<t>

Source: Internet
Author: User

Tqueue and Tstack, one is the list of queues, one is the stack list; One is advanced first out, one is advanced later out.

Tqueue has three main methods, one attribute:

Enqueue (Row), dequeue (column), Peek (see the next element to be out);

Count (total elements).

This example effect chart:

Code files:Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;
Type
TForm1 = Class (Tform)
Memo1:tmemo;
Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses generics.collections; {Delphi 2009 New generic Container Unit}
Type
TRec = Record
name:string;
Age:word;
End
Var
queue:tqueue<trec>; {Defines a generic Tqueue class that specifies the TREC record to use for the definition above}
Establish
Procedure Tform1.formcreate (Sender:tobject);
Begin
Queue: = Tqueue<trec>. Create;
Memo1.clear;
Button1.caption: = button1.caption + ' row ';
Button2.caption: = button2.caption + ' out ';
Button3.caption: = button3.caption + ' next column ... ';
End
Release
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Queue.free;
End
{row: Enqueue}
Procedure Tform1.button1click (Sender:tobject);
Var
Rec:trec;
Begin
Rec. Name: = Stringofchar (Char (+ Random (26)), 3);
Rec. Age: = Random (150);
Queue.enqueue (REC);
Text: = Format (' Total current queue members:%d ', [Queue.count]);
{Let Memo1 match display}
MEMO1.LINES.ADD (Format ('%s,%d ', [Rec.] Name, rec. Age]));
End
{column: Dequeue}
Procedure Tform1.button2click (Sender:tobject);
Var
Rec:trec;
Begin
If Queue.count = 0 then Exit;
Rec: = Queue.dequeue;
ShowMessage (Format ('%s,%d ', [Rec.] Name, rec. Age]));
Text: = Format (' Total current queue members:%d ', [Queue.count]);
{Let Memo1 match display}
Memo1.Lines.Delete (0);
End
{Next column element: Peek}
Procedure Tform1.button3click (Sender:tobject);
Var
Rec:trec;
Begin
If Queue.count = 0 then Exit;
Rec: = Queue.peek;
ShowMessage (Format ('%s,%d ', [Rec.] Name, rec. Age]));
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.