1. 一个简单的
type
PMyRec = ^MyRec; {把 PMyRec 定义为下面 MyRec 结构的指针类型}
MyRec = record
Name: string[8];
Age : Word;
Link: PMyRec; {结构中同时包含同类型的指针, 用于链接其他同类结构}
end;
var
R1,R2,R3,R4: MyRec;
{把 R1、R2、R3、R4 够建成一个环环相扣的"链", 这就是一个简单的"链表"}
procedure TForm1.FormCreate(Sender: TObject);
begin
R4.Name := '李四';
R4.Age := 16;
R4.Link := nil;
R3.Name := '张三';
R3.Age := 61;
R3.Link := @R4;
R2.Name := '钱二';
R2.Age := 24;
R2.Link := @R3;
R1.Name := '赵一';
R1.Age := 42;
R1.Link := @R2;
end;
{现在 , 通过 R1 即可以访问整个链; 但这个链是单向的, 所以叫"单向链表"}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(R1.Name); {赵一}
ShowMessage(R1.Link^.Name); {钱二}
ShowMessage(R1.Link^.Link^.Name); {张三}
ShowMessage(R1.Link^.Link^.Link^.Name); {李四}
end;
{通过结构指针读取数据可以省略 ^; 所以 Button1Click 可以简写为:}
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(R1.Name); {赵一}
ShowMessage(R1.Link.Name); {钱二}
ShowMessage(R1.Link.Link.Name); {张三}
ShowMessage(R1.Link.Link.Link.Name
2.单向链表的添加、删除与遍历
implementation
{$R *.dfm}
type
PMyRec = ^MyRec;
MyRec = record
Name: string[8];
Age : Word;
Link: PMyRec;
end;
var
CurrentNode, FirstNode: PMyRec;
{建立链表}
procedure TForm1.FormCreate(Sender: TObject);
begin
New(FirstNode);
FirstNode.Name := '李四';
FirstNode.Age := 44;
FirstNode.Link := nil;
CurrentNode := FirstNode;
New(FirstNode);
FirstNode.Name := '张三';
FirstNode.Age := 33;
FirstNode.Link := CurrentNode;
CurrentNode := FirstNode;
New(FirstNode);
FirstNode.Name := '钱二';
FirstNode.Age := 22;
FirstNode.Link := CurrentNode;
CurrentNode := FirstNode;
New(FirstNode);
FirstNode.Name := '赵一';
FirstNode.Age := 11;
FirstNode.Link := CurrentNode;
CurrentNode := FirstNode;
Button1.Caption := '遍历链表';
Button2.Caption := '插入节点';
Button3.Caption := '删除节点';
Button4.Caption := '删除链表';
Memo1.Align := alLeft;
Memo1.ScrollBars := ssVertical;
end;
{遍历链表节点}
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
CurrentNode := FirstNode;
while Assigned(CurrentNode) do
begin
Memo1.Lines.Add(CurrentNode.Name + #9 + IntToStr(CurrentNode.Age));
CurrentNode := CurrentNode.Link;
end;
end;
{插入节点}
procedure TForm1.Button2Click(Sender: TObject);
var
NewNode: PMyRec;
begin
{譬如是插入在第二个节点后面}
CurrentNode := FirstNode.Link;
New(NewNode);
NewNode.Name := 'AAA';
NewNode.Age := 100;
{前后连起来}
NewNode.Link := CurrentNode.Link;
CurrentNode.Link := NewNode;
{调用遍历}
Button1.Click;
end;
{删除节点}
procedure TForm1.Button3Click(Sender: TObject);
begin
{譬如是删除第二个节点}
CurrentNode := FirstNode.Link; {让 CurrentNode 指向第二个节点}
if Assigned(CurrentNode.Link) then {如果 CurrentNode 还有下一个}
begin
FirstNode.Link := CurrentNode.Link; {不能断了链}
Dispose(CurrentNode); {释放用 New 申请的内存}
end;
Button1.Click;
end;
{销毁链表, 释放所有节点}
procedure TForm1.Button4Click(Sender: TObject);
begin
CurrentNode := FirstNode; {从链头向下删除}
while Assigned(FirstNode) do
begin
CurrentNode := FirstNode;
FirstNode := CurrentNode.Link;
Dispose(CurrentNode);
end;
Button1.Click;
end;
3.双向链表
type
PMyRec = ^MyRec;
MyRec = record
Name: string[8];
Age : Word;
Prev,Next: PMyRec; {上一个与下一个的结构指针}
end;
var
CurrentNode: PMyRec;
{建立双向链表}
procedure TForm1.FormCreate(Sender: TObject);
var
PrevNode: PMyRec;
i: Integer;
begin
PrevNode := nil;
for i := 65 to 71 do {建七个节点, 准备用 AA, BB, CC, DD, EE, FF, GG 来指定 MyRec.Name }
begin
New(CurrentNode);
CurrentNode.Name := Chr(i) + Chr(i);
CurrentNode.Age := i;
CurrentNode.Prev := PrevNode;
if Assigned(PrevNode) then PrevNode.Next := CurrentNode;
PrevNode := CurrentNode;
end;
Button1.Caption := '上一个';
Button2.Caption := '下一个';
end;
{上一个}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(CurrentNode.Prev) then
begin
CurrentNode := CurrentNode.Prev;
Text := CurrentNode.Name + #44 + IntToStr(CurrentNode.Age);
end else Text := '这是第一个节点';
end;
{下一个}
procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned(CurrentNode.Next) then
begin
CurrentNode := CurrentNode.Next;
Text := CurrentNode.Name + #44 + IntToStr(CurrentNode.Age);
end else Text := '这是最后一个节点';
end;
end