请问如何实现队列TQueue的进队出队?

cococo2004 2004-12-19 04:01:15
请问如何实现队列的进队出队?
谢谢?
代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Contnrs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
MyQueue:TQueue;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
a:string;
begin
a:='Hello,world!';
MyQueue.Push(@a);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
MyQueue:=TQueue.Create;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
S:^String;
begin
S:=MyQueue.Pop;
Edit1.Text:=^S;
end;

end.

但是并没有成功在edit1里面出现'Hello,world!'
请问问题出在哪里?
谢谢
...全文
624 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly_liuyang 2005-01-30
  • 打赏
  • 举报
回复
用PCHar空间才还的
String不多适合的,除非用New来处理

http://lysoft.7u7.net
chinaandys 2005-01-29
  • 打赏
  • 举报
回复
好贴,学习
Rex_love_Burger 2005-01-29
  • 打赏
  • 举报
回复
例:
var
MyQueue:TQueue; //声明队变量
ARecord:^MyName; //一个指针
i:integer;
ss:String;
{初始化部分}
MyQueue:=TQueue.Create();
{Push部分}
for i=0 to 10 do
begin
New(ARecord);//给指针空间
ARecord^.filename:=IntToStr(i);
MyQueue.Push(ARecord);//进队
end;
{Pop部分}
for :=0 to (MyQueue.Count-1) do
begin
ARecord:=MyQueue.Pop;//出队
ss:=ARecord^.filename;
Dispose(ARecord); //清空指针
end;

{释放队的资源}
MyQueue.Free;  
yeliming 2004-12-28
  • 打赏
  • 举报
回复
补充,我用的是D7
yeliming 2004-12-28
  • 打赏
  • 举报
回复
不用那么麻烦吧?
VCL的Contnrs.pas里有个现成的TObjectQueue class。
还有TObjectStack。
progray 2004-12-28
  • 打赏
  • 举报
回复
mark
diecode 2004-12-21
  • 打赏
  • 举报
回复
结点要用全局变量或者动态分配空间,TQUEUE维护的是指针
Kevin_Lmx 2004-12-19
  • 打赏
  • 举报
回复
type
PMyRecord = ^TMyRecord;
TMyRecord = record
filename :String;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
MyQueue:TQueue; //声明队变量
ARecord:PMyRecord; //一个指针
ss:String;
{初始化部分}
begin
MyQueue:=TQueue.Create();
{Push部分}
New(ARecord);//给指针空间
ARecord^.filename:='abcdefg';
MyQueue.Push(ARecord);//进队
{Pop部分}
ARecord:=MyQueue.Pop;//出队
Edit1.TExt:=ARecord^.filename;
Dispose(ARecord); //清空指针

{释放队的资源}
MyQueue.Free;
end;
麻子 2004-12-19
  • 打赏
  • 举报
回复
unit Queue;

interface

uses Windows;

function IsEmpty(): Boolean;
procedure EnQueue(pt: TPoint);
function OutQueue(): TPoint;

implementation

const
QueueLen = 10;

var
Pos: array[0..QueueLen] of TPoint;
Frist: Integer = 0;
Last: Integer = 0;

procedure EnQueue(pt: TPoint);
var
tmp: Integer;
begin
tmp := (Last+1)mod(QueueLen+1);
if (tmp = Frist) then OutQueue;
Last := tmp;
Pos[Last] := pt;
end;

function OutQueue(): TPoint;
begin
if (Last = Frist) then
begin
Result.X := 0;
Result.Y := 0;
end else
begin
Frist := (Frist+1)mod(QueueLen+1);
Result := Pos[Frist];
end;
end;

function IsEmpty(): Boolean;
begin
Result := (Last=Frist);
end;

end.

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧