近来有好多同学问到在delphi中使用链表的问题,在这里我就简单来说说吧.
对C语言版的链表大家都很熟识了,我们就先来看看C关于链表的定义,这里以队列为例:
typedef struct Qnode{ //定义数据元素类型
int data;
struct Qnode *next;
}Qnode,*QueuePtr;
typedef struct{ //定义队列
QueuePtr front;
QueuePtr rear;
}LinkQueue;
这里我们只要把握一点就行: next,front,rear三个变量都是Qnode类指针.QueuePtr front等价于Qnode *front。
下面看看delphi中的定义:
type
link=^node; //定义链表数据类型
node=record
data:string;
next:link;
end;
type
LinkQueue=record //定义队列(具有头指针和尾指针)
front:link;
rear:link;
end;
这里的定义和C的定义在本质上的一样的,只是表现形式有点区别而已,link=^node表明link是node类指针,在理解上我们可能这样定义:
type
//定义链表数据类型
node=record
data:string;
next:^node;//这里把link换成了^node
end;
这个只是理解上的定义,编译时会产生如下错误的:
[错误] Unit1.pas(28): Type 'node' is not yet completely defined
node类型还没有完全定义,这里就好像在C中把struct Qnode *next写成 Qnode *next时会生产 "identifier 'Qnode'"错误是一样道理的。C中在Qnode前加个struct就可以用还没完整定义的Qnode来定义变量,同样在delphi中用link=^node来说明就可能用link来代替^node而不会产生编译错误,这只是不同语言在语法上的规定而已,我们不必去理会那么多。总之我们记住一点就可以:next,front,rear三个变量都是指针类型,至于如何去定义,不同的语言有不同的规定。
下面是用delphi写的一个队列例子,用到的组件有3个Button,一个Edit,一个Memo。第一个按钮:向队列中增加一个元素,其中Data为edit1.text;第二个按钮:删除队列中的一个元素;第三个按钮:把队列中的内容显示在Memo上
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Edit1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
link=^node; //定义链表数据类型
node=record
data:string;
next:link;
end;
LinkQueue=record //定义队列(具有头指针和尾指针)
front,rear:link;
end;
var
Form1: TForm1;
MyQueue:LinkQueue; //定义队列变量
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); //
var p:link;
begin
new(p); //申请空间
p.data:=edit1.Text;
p.next:=nil;
if MyQueue.front=nil then //队列为空时,让头指针和尾指针都指向p
begin
MyQueue.Front:=p;
MyQueue.rear:=p;
end
else //队列不为空时,向队列尾部增加p,并使rear指向p(最后一个元素)
begin
MyQueue.rear.next:=p;
MyQueue.rear:=p;
end;
end;
procedure TForm1.Button3Click(Sender: TObject); //显示队列元素
var p:link;
begin
memo1.Lines.Clear;
p:=MyQueue.front;
while p<>nil do
begin
memo1.Lines.Add(p.data);
p:=p.next;
end;
end;
procedure TForm1.Button2Click(Sender: TObject); //从队列中删除一个元素
var p:link;
begin
if myqueue.front<>nil then
begin
p:=MyQueue.front;
MyQueue.front:=MyQueue.front.next;
dispose(p);
end;
end;
end.
其实Delphi中有专门的链表类如Tlist,Tqueue,Tstack等等,使用很方便的,对于一般的链表问题用这3个类就可以了,其具体的使用方法以后有时间再跟大家说说,大家可以先看看Delphi帮助。