链表测试

复制代码
代码


PMyList
= ^TMyList;
TMyList
= record
Text:
string;
Previous: PMyList;
Next: PMyList;
end;

var
FHead: PMyList;
FLast: PMyList;
CurrIndex: Integer;
FList: TList;

procedure TForm1.btn_1Click(Sender: TObject);
begin
//创建链表头
GetMem(FHead, SizeOf(TMyList));
FHead.Text :
= Edit1.Text;
FHead.Previous :
= nil;
FHead.Next :
= nil;
FLast :
= FHead;
end;

procedure TForm1.btn_2Click(Sender: TObject);
var
vCur: PMyList;
begin
//添加新元素
GetMem(vCur, SizeOf(TMyList));
vCur.Text :
= Edit1.Text;
FLast.Next :
= vCur;
vCur.Previous :
= FLast;
vCur.Next :
= nil;
FLast :
= vCur;
end;

procedure TForm1.btn_3Click(Sender: TObject);
var
vCur: PMyList;
begin
//显示所有元素
mmo1.Clear;
vCur :
= FHead;
while vCur <> nil do
begin
mmo1.Lines.Add(vCur.Text);
vCur :
= vCur.Next;
end;
end;

procedure TForm1.btn_4Click(Sender: TObject);
var
vCur: PMyList;
begin
mmo1.Clear;
vCur :
= FLast;
//反显所有元素
while vCur <> nil do
begin
mmo1.Lines.Add(vCur.Text);
vCur :
= vCur.Previous;
end;
end;

procedure TForm1.btn_5Click(Sender: TObject);
var
vCur, p: PMyList;
begin
//销毁链表
p :
= FHead;
while p <> nil do
begin
vCur :
= p;
p :
= vCur.Next;
FreeAndNil(vCur);
end;
end;
复制代码

 

posted @   一 点  阅读(209)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示