链表测试
代码
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;
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步