构造自己的双向链表
双向链表类
代码
{*******************************************************}
{ }
{ Module Name: unitLinkClass.pas }
{ Author Ming.z }
{ Creation Date 2008-12-13 }
{ LastUpdateDate 2009-02-27 }
{ LastUpdateDate 2010-12-28 }
{ }
{*******************************************************}
unit unitLinkClass;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms;
type
PRecLinkNode = ^RecLinkNode;
RecLinkNode = record
NodeMsg: String;
Counter: Integer;
Previous: PRecLinkNode;
Next: PRecLinkNode;
end;
TLinkClass = class
private
FNode,LNode:PRecLinkNode;
public
Constructor Create;
Destructor Destroy; override;
procedure AddToHeadNode(const S:String);
procedure AddToTailNode(const S:String);
procedure DeleteHeadNode;
procedure DeleteTailNode;
procedure BakupMsg;
public
property HeadNode: PRecLinkNode read FNode;
property TailNode: PRecLinkNode read LNode;
end;
var
LinkClass: TLinkClass;
implementation
Constructor TLinkClass.Create;
begin
inherited;
FNode := nil;
LNode := nil;
end;
Destructor TLinkClass.Destroy;
var
p: PRecLinkNode;
begin
//BakupMsg;
while FNode <> nil do
begin
p := FNode;
FNode := FNode.next;
dispose(p);
end;
inherited;
end;
procedure TLinkClass.AddToHeadNode(const S:String);
var
p: PRecLinkNode;
begin
new(p);
if FNode = nil then
begin
LNode:=FNode;
p.NodeMsg:=S;
p.Next:=FNode; //nil
p.Previous:=FNode; //nil
FNode:=p;
LNode:=p;
end
else
begin
FNode.Previous:=p;
p.NodeMsg:=S;
p.Next:=FNode;
p.Previous:=nil;
FNode:=p;
end;
end;
procedure TLinkClass.AddToTailNode(const S:String);
var
p: PRecLinkNode;
begin
new(p);
if FNode = nil then
begin
LNode:=FNode;
p.NodeMsg:=S;
p.Next:=FNode; //nil
p.Previous:=FNode; //nil
FNode:=p;
LNode:=p;
end
else
begin
LNode.Next:=p;
p.NodeMsg:=S;
p.Next:=nil;
p.Previous:=LNode;
LNode:=p;
end;
end;
procedure TLinkClass.DeleteHeadNode;
var
p: PRecLinkNode;
begin
p := FNode;
if p = nil then exit;
if (p.Next <> nil) then
begin
FNode := FNode.Next;
FNode.Previous := nil;
dispose(p);
end
else
begin
dispose(p);
FNode := nil;
LNode := nil;
end;
end;
procedure TLinkClass.DeleteTailNode;
var
p: PRecLinkNode;
begin
p := LNode;
if p = nil then exit;
if (p.Previous <> nil) then
begin
LNode := LNode.Previous;
LNode.Next := nil;
dispose(p);
end
else
begin
dispose(p);
LNode := nil;
FNode := nil;
end;
end;
procedure TLinkClass.BakupMsg;
var
ls: TStringList;
p: PRecLinkNode;
sSavePath: string;
begin
sSavePath := ExtractFilePath(Application.ExeName)
+ FormatDateTime('YYYYMMDD_HHMMSS', Now) + 'BakMsg' + '.txt';
p := LinkClass.HeadNode;
ls := TStringList.Create;
try
while p <> nil do
begin
ls.Add(p.NodeMsg);
p := p.next;
end;
ls.SaveToFile(sSavePath);
finally
FreeAndNil(ls);
end;
end;
initialization
LinkClass := TLinkClass.create;
finalization
LinkClass.Free;
end.
调用
代码
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 5 do
begin
//LinkClass.AddToTailNode(IntToStr(i));
LinkClass.AddToHeadNode(IntToStr(i));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
p: PRecLinkNode;
begin
p := LinkClass.HeadNode;
while p <> nil do
begin
Memo1.Lines.Add(p.NodeMsg);
p := p.Next;
end;
p := LinkClass.TailNode;
while p <> nil do
begin
Memo1.Lines.Add(p.NodeMsg);
p := p.Previous;
end;
end;
procedure TForm1.btnDeleteHeadClick(Sender: TObject);
begin
LinkClass.DeleteHeadNode;
end;
procedure TForm1.btnDeleteTailClick(Sender: TObject);
begin
LinkClass.DeleteTailNode;
end;
procedure TForm1.Memo1DblClick(Sender: TObject);
begin
Memo1.Clear;
end;
procedure TForm1.btnBackUpClick(Sender: TObject);
begin
LinkClass.BakupMsg;
end;