泛型容器单元(Generics.Collections)[2]: TQueue<T> 队列列表
TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出.
TQueue 主要有三个方法、一个属性:
Enqueue(入列)、Dequeue(出列)、Peek(查看下一个要出列的元素);
Count(元素总数).
本例效果图:

代码文件:
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 新增的泛型容器单元} type
TRec = record
Name: string;
Age: Word;
end; var
Queue: TQueue<TRec>; {定义一个泛型 TQueue 类, 这指定了要用于上面定义的 TRec 记录} {建立}
procedure TForm1.FormCreate(Sender: TObject);
begin
Queue := TQueue<TRec>.Create; Memo1.Clear;
Button1.Caption := Button1.Caption + ' 入列';
Button2.Caption := Button2.Caption + ' 出列';
Button3.Caption := Button3.Caption + ' 下一个出列的...';
end; {释放}
procedure TForm1.FormDestroy(Sender: TObject);
begin
Queue.Free;
end; {入列: Enqueue}
procedure TForm1.Button1Click(Sender: TObject);
var
rec: TRec;
begin
rec.Name := StringOfChar(Char( + Random()), );
rec.Age := Random();
Queue.Enqueue(rec);
Text := Format('当前队列成员总数: %d', [Queue.Count]); {让 Memo1 配合显示}
Memo1.Lines.Add(Format('%s, %d', [rec.Name, rec.Age]));
end; {出列: Dequeue}
procedure TForm1.Button2Click(Sender: TObject);
var
rec: TRec;
begin
if Queue.Count = then Exit;
rec := Queue.Dequeue;
ShowMessage(Format('%s, %d', [rec.Name, rec.Age]));
Text := Format('当前队列成员总数: %d', [Queue.Count]); {让 Memo1 配合显示}
Memo1.Lines.Delete();
end; {下一个出列的元素: Peek}
procedure TForm1.Button3Click(Sender: TObject);
var
rec: TRec;
begin
if Queue.Count = then Exit;
rec := Queue.Peek;
ShowMessage(Format('%s, %d', [rec.Name, rec.Age]));
end; end.
窗体文件:
object Form1: TForm1
Left =
Top =
Caption = 'Form1'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch =
TextHeight =
object Memo1: TMemo
Left =
Top =
Width =
Height =
Align = alLeft
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Courier New'
Font.Style = []
Lines.Strings = (
'Memo1')
ParentFont = False
ScrollBars = ssBoth
TabOrder =
ExplicitHeight =
end
object Button1: TButton
Left =
Top =
Width =
Height =
Caption = 'Button1'
TabOrder =
OnClick = Button1Click
end
object Button2: TButton
Left =
Top =
Width =
Height =
Caption = 'Button2'
TabOrder =
OnClick = Button2Click
end
object Button3: TButton
Left =
Top =
Width =
Height =
Caption = 'Button3'
TabOrder =
OnClick = Button3Click
end
end
泛型容器单元(Generics.Collections)[2]: TQueue<T> 队列列表的更多相关文章
- 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表
TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个 ...
- Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...
- C#中泛型容器Stack<T>
我以前都是学出c,c++,这个学期开始学c#有点不适应,在编程中遇到些问题,所以自己在网上查了些资料,翻了一下书,写一些总结. 关于c#中Stack<T>泛型容器: <1>st ...
- 从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?
这一章节我们继续类型擦除的话题,我们将通过对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题? 1.数组 package com.ray.ch13; public class Test { pub ...
- 泛型的排序问题(Collections.sort及Comparable的应用)
一.前言 java中对泛型(集合类型)排序的问题,主要采用了两张方式一种是对要排序的实体类,实现Comparable接口,另一种方式,Collections集合工具类进行排序. 二.实现Comp ...
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...
- 8、泛型程序设计与c++标准模板库2.4列表容器
列表容器主要用于存放链表,其中的链表是双向链表,可以从任意一端开始遍历.列表容器是需要按顺序访问的容器.另外,列表容器不支持随机访问迭代器,因此某些算法不能适合于列表容器.列表容器还提供了另一种操作- ...
- Python collections系列之单向队列
单向队列(deque) 单项队列(先进先出 FIFO ) 1.创建单向队列 import queue q = queue.Queue() q.put(') q.put('evescn') 2.查看单向 ...
- Python collections系列之双向队列
双向队列(deque) 一个线程安全的双向队列 1.创建一个双向队列 import collections d = collections.deque() d.append(') d.appendle ...
随机推荐
- 【hadoop2.6.0】数据丢失问题解决
想自己走一遍从代码到运行的流程,结果各种错,郁闷啊. 问题① http://localhost:50070/ 里面一进去就告诉我块丢了.... 解决: bin/hadoop fsck -delete ...
- Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 编译QtAV工程库
去https://github.com/wang-bin/QtAV下载源代码 去https://sourceforge.net/projects/qtav/files/depends/QtAV-dep ...
- July 1st, Week 27th Friday, 2016
It does not do to dwell on dreams, and forget to live. 不要生活在梦里,不要沉醉于空想而疏忽了生活. Stand straightly, and ...
- python基础——错误处理
python基础——错误处理 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数 ...
- 阿里云的RDS 查看binlog日志的方法
按时间点反后台备份的binlog日志从阿里云导出来,然后用mysqlbinlog查看日志内容: # mysqlbinlog -vv --base64-output=decode-rows mysql- ...
- web端跨域调用webapi
在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...
- Error parsing 'file:///media/RHEL_5.5\\ x86_64\\ DVD/Server'
Error parsing 'file:///media/RHEL_5.5\\ x86_64\\ DVD/Server' http://lindows.iteye.com/blog/456637 ht ...
- Shell脚本获取C语言可执行程序返回值
#!/bin/sh #./test是c程序,该程序 返回0 ./test OP_MODE=$? echo $OP_MODE # $? 显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误.
- 监听报错 TNS-00525: Insufficient privilege for operation 11gR2 + 连接报错ORA-12537: TNS:connection closed
1.TNS-00525: Insufficient privilege for operation Started with pid= Listening on: (DESCRIPTION=(ADDR ...