type
point=^node;
node=record
data:integer;
next:point;
end;
var h1,h2,h:point;
procedure prt(p:point); //打印
链表begin
p:=p^.next;
while p<>nil do
begin
write(p^.data,' ');
p:=p^.next;
end;
writeln;
end;
procedure creat(var h:point); //建立链表
var x:integer; p,q:^node;
begin
writeln('请输入升序的数,负数结束:');
new(h);
p:=h;
read(x);
while(x>=0)do
begin
new(q);
q^.data:=x;
p^.next:=q;
p:=q;
read(x);
end;
p^.next:=nil;
end;
function merge_link(var p,q:point):point; //升序合并二个升序链表
var h,w:^node;
begin
w:=p; p:=p^.next; dispose(w); //回收一个头结点,p指向首个数据结点
w:=q; h:=q; q:=q^.next; //h:合并后的头结点,q指向首个数据结点
while (p<>nil)and(q<>nil) do //当二个链表都不空时
if(p^.data<q^.data) then //选一个小的结点
begin
w^.next:=p; //把小结点链入
p:=p^.next; //跳过此结点
w:=w^.next; //w指向当前合并后链表的尾结点
end
else
begin //下面三行作用同上
w^.next:=q;
q:=q^.next;
w:=w^.next;
end;
if p<>nil then w^.next:=p; //将未完的链表接入
if q<>nil then w^.next:=q; //将未完的链表接入
merge_link:=h; //返回合并后的链表头指针
end;
begin
creat(h1);
creat(h2);
h:=merge_link(h1,h2);
writeln('合并后的链表:');
prt(h);