# 《Delphi 算法与数据结构》学习与感悟[10]: 双向链表

```unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}

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.

```
原文作者：算法小白
原文地址: https://www.cnblogs.com/del/archive/2008/03/19/1113075.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。