如果在结构中再拿出 4 个字节的地址空间指向上一个节点, 就成了双向链表了.
本例效果图:
本例效果图:
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.