PASCAL中怎么建立一个双向的链表1
发布网友
发布时间:2023-10-23 17:49
我来回答
共3个回答
热心网友
时间:2024-12-11 16:47
type pointer=^node;
node=record
data:integer;
front,next:pointer;
end;
var head,tail:pointer; n:integer;
procere creat(var head,tail:pointer; var n:integer); {jianli caozuo}
var p:pointer;
begin
n:=0; new(p);
readln(p^.data); p^.front:=nil; p^.next:=nil;
while p^.data<>-1 do
begin inc(n);
if n=1 then begin head:=p; tail:=p; end
else begin p^.front:=tail; tail^.next:=p; p^.next:=nil;
tail:=p; end;
new(p);
readln(p^.data);
end;end;
procere print(head,tail:pointer);
var p:pointer;
begin
p:=head;
writeln('tou wei:');
while p<>nil do
begin
write(p^.data:6);
p:=p^.next;
end;
writeln;
end;
begin
creat(head,tail,n);
print(head,tail);
dele(head,tail,n,2);
print(head,tail);
end.
热心网友
时间:2024-12-11 16:47
指针实现的双向链表:
type
node = ^list;
list = record
val:integer;
prev,next:node;
end;
{尾部插入}
procere append(var head:node; val:integer);
var
p,t:node;
begin
t := head;
p := nil;
while t <> nil do
begin p := t; t := t^.next; end;
if p <> nil then
begin new(p^.next); t := p^.next end
else
begin new(head); t := head; end;
t^.prev := p;
t^.next := nil;
t^.val := val;
end;
{遍历输出}
procere traversal(head:node);
begin
while head <> nil do
begin
write(head^.val, ' ');
head := head^.next;
end;
writeln;
end;
{反序}
procere reverse(var head:node);
var l,t:node;
begin
l := head;
while l <> nil do
begin
t := l^.next;
l^.next := l^.prev;
l^.prev := t;
head := l;
l := t;
end;
end;
{销毁}
procere destroy(head:node);
var t:node;
begin
while head <> nil do
begin
t := head;
head := head^.next;
dispose(t);
end;
end;
var
head:node = nil;
i:integer;
begin
for i := 1 to 10 do
append(head, i);
traversal(head);
reverse(head);
traversal(head);
destroy(head);
readln;
end.
热心网友
时间:2024-12-11 16:48
var
i,j,n:longint;
pre,now,x,y:array[0..1000] of longint;begin
readln(n);
for i:=1 to n do
begin
readln(x[i*2-1],y[i*2-1]);
pre[i*2-1]:=now[x[i*2-1]];
now[x[i*2-1]]:=i*2-1; x[i*2]:=y[x*2-1]; y[x*2]:=x[i*2-1];
pre[i*2]:=now[x[i*2]];
now[x[i*2]]:=i*2;
end;
end. 是这样的吗?