delphi 链表问题,请教大侠,希望实现的功能:通过一个循环量添加链表节点,这个值未知,
procedure TForm1.Button9Click(Sender: TObject);
type
PTrecord=^Trecord ;
Trecord=record
arr:array of byte;
l:integer;
ip:string;
tmpbusno:string;
link:PTrecord;
end;
var
i,j,k,n,l,z:integer;
str,str1,tmpstr,ip,tmpbusno:string;
arraybye,arraybye1:array of byte;
CurrentNode, FirstNode,tmpnode:PTrecord ;
begin
str1:='AA051C0001000000000000000000000000000000003132000000000000850055';
j:=length(str1) div 2;
setlength(arraybye,j);
i:=1;
for k:=0 to j-1 do
begin
arraybye[k]:=hextoint(copy(str1,i,2));
i:=i+2;
end;
//初始化链表头节点
New(FirstNode);
setlength(FirstNode.arr,length(arraybye));
movememory(FirstNode.arr,arraybye,length(arraybye));
FirstNode.l := 10;
FirstNode.ip := '127.0.0.1';
FirstNode.tmpbusno := '00000';
FirstNode.Link := nil;
CurrentNode := FirstNode;
l:=arraybye[2]+256*arraybye[3]+4;
//循环增加结点
for k:=0 to strtoint(edit1.Text)-1 do
begin
New(FirstNode);
setlength(FirstNode.arr,length(arraybye));
movememory(FirstNode.arr,arraybye,length(arraybye));
FirstNode.l := k+1;
FirstNode.ip := '200.200.1.117';
FirstNode.tmpbusno := '10089';
FirstNode.Link := CurrentNode;
CurrentNode := FirstNode;
end;
n:=0;
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //获取链表长度
begin
n:=n+1;
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add(inttostr(n));
CurrentNode := FirstNode; //指向倒数第二个节点 个人对那个是链表的头很混乱。。。
for k:=0 to n-3 do
begin
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add(inttostr(currentNode.l));
tmpnode:=CurrentNode;
if Assigned(tmpnode.link) then //删除该节点,然后将链表重新连接
begin
CurrentNode:=tmpnode.link; {这句编译时,显示没有编译,从不运行???}
Dispose(tmpnode);
end;
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //遍历链表
begin
memo1.lines.add(inttostr(currentNode.l));
CurrentNode := CurrentNode.Link;
end;
end;
好的话,继续加分 展开
procedure TForm1.Button1Click(Sender: TObject);
type
PTrecord=^Trecord ;
Trecord=record
arr:array of byte;
l:integer;
ip:string;
tmpbusno:string;
link:PTrecord;
end;
var
i,j,k,n,l,z:integer;
str,str1,tmpstr,ip,tmpbusno:string;
arraybye,arraybye1:array of byte;
CurrentNode, FirstNode,tmpnode:PTrecord ;
begin
str1:='AA051C0001000000000000000000000000000000003132000000000000850055';
j:=length(str1) div 2;
setlength(arraybye,j);
i:=1;
for k:=0 to j-1 do
begin
arraybye[k]:=hextoint(copy(str1,i,2));
i:=i+2;
end;
//初始化链表头节点
New(FirstNode);
setlength(FirstNode.arr,length(arraybye));
movememory(FirstNode.arr,arraybye,length(arraybye));
FirstNode.l := 10;
FirstNode.ip := '127.0.0.1';
FirstNode.tmpbusno := '00000';
FirstNode.Link := nil;
CurrentNode := FirstNode;
l:=arraybye[2]+256*arraybye[3]+4;
//循环增加结点
for k:=0 to strtoint(edit1.Text)-1 do
begin
New(FirstNode);
setlength(FirstNode.arr,length(arraybye));
movememory(FirstNode.arr,arraybye,length(arraybye));
FirstNode.l := k+1;
FirstNode.ip := '200.200.1.117';
FirstNode.tmpbusno := '10089';
FirstNode.Link := CurrentNode; //倒顺序建立啊
CurrentNode := FirstNode;
end;
memo1.lines.add('建立完成');
while Assigned(CurrentNode) do //遍历链表
begin
memo1.lines.add(inttostr(currentNode.l));
CurrentNode := CurrentNode.Link;
end;
n:=0;
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //获取链表长度
begin
n:=n+1;
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add('链表长度:'+inttostr(n));
CurrentNode := FirstNode; //指向倒数第二个节点 个人对那个是链表的头很混乱。。。
for k:=0 to n-3 do
begin
tmpnode:=CurrentNode;//记录倒数第三个
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add('倒数第二个是:'+inttostr(currentNode.l));
//tmpnode:=CurrentNode;
if Assigned(tmpnode.link) then //删除该节点,然后将链表重新连接
begin
tmpnode.link:=CurrentNode.link; {这句编译时,显示没有编译,从不运行???CurrentNode从来不被使用,被优化掉了,少加了.link}
Dispose(CurrentNode);
end;
memo1.lines.add('删除后:');
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //遍历链表
begin
memo1.lines.add(inttostr(currentNode.l));
CurrentNode := CurrentNode.Link;
end;
end;
我现在是一直向尾部添加数据,这叫倒序建立?菜鸟啊,请不要取笑。那如果是顺序建立呢。应该怎么做。
然后如果我要处理头结点的下一个结点,应该怎么做呢,
现在你这种情况的确能处理链表长度大于3的情况,但如果小于3就会出错了。
非常感谢!!!!!!!!!
正常情况,应该是不能记录第三个节点,然后再来连接的,所以删除那部分,能不能顺序建立后,重新处理一下。谢谢!!!
废话,你要删除倒数第二个,当然长度小于3就出错了
删除倒数第二个的问题,要分类判断,如果长度》3,按我的方法,如果=2,是一个方法,你应该会的,如果<2,提示不够2
顺序建立的话,新结点在链表尾部
//初始化链表头节点
New(FirstNode);
setlength(FirstNode.arr,length(arraybye));
movememory(FirstNode.arr,arraybye,length(arraybye));
FirstNode.l := 0;
FirstNode.ip := '127.0.0.1';
FirstNode.tmpbusno := '00000';
FirstNode.Link := nil;
CurrentNode := FirstNode;
l:=arraybye[2]+256*arraybye[3]+4;
//循环增加结点
LastNode:=FirstNode; k:=0;
for k:=0 to strtoint(edit1.Text)-1 do
begin
New(CurrentNode);
setlength(CurrentNode.arr,length(arraybye));
movememory(CurrentNode.arr,arraybye,length(arraybye));
CurrentNode.l := k+1;
CurrentNode.ip := '200.200.1.117';
CurrentNode.tmpbusno := '10089';
CurrentNode.link:=nil;
LastNode.link:= CurrentNode;
LastNode:=CurrentNode;
end;
memo1.lines.add('建立完成');
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //遍历链表
begin
memo1.lines.add(inttostr(currentNode.l));
CurrentNode := CurrentNode.Link;
end;
n:=0;
CurrentNode := FirstNode;
while Assigned(CurrentNode) do //获取链表长度
begin
n:=n+1;
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add('链表长度:'+inttostr(n));
CurrentNode := FirstNode; //指向倒数第二个节点 个人对那个是链表的头很混乱。。。
for k:=0 to n-3 do
begin
tmpnode:=CurrentNode;//记录倒数第三个
CurrentNode := CurrentNode.Link;
end;
memo1.lines.add('倒数第二个是:'+inttostr(currentNode.l));
//tmpnode:=CurrentNode;
if Assigned(tmpnode.link) then //删除该节点,然后将链表重新连接
begin
tmpnode.link:=CurrentNode.link; {这句编译时,显示没有编译,从不运行???CurrentNode从来不被使用,被优化掉了,少加了.link}
Dispose(CurrentNode);
end;