实现链表遍历,如果满足条件就插入新项到表尾,并重新遍历,测试时,加上path[3]正确,但加上path[4]就不对了,请指教 point=^info;
info=record
arrpascity:array [1..20] of integer;
no,scity,ecity,stime,etime,coday,cotime,comoney:integer;
track,pascity:string;
next,prior:point;
end;
info1=record
arrpascity:array [1..20] of integer;
scity,ecity,stime,etime,coday,cotime,comoney:integer;
track,pascity:string;
end;var
Form1: TForm1;
head,til:point;
pathnum:integer;
path:array [1..10000] of info1;
judge:array [1..10000,1..10000] of boolean;
implementation
{$R *.dfm}procedure TForm1.buildlink;
var
j,h:integer;
m,t:point;
begin
new(head);
new(t);
head.no:=1;
head.scity:=path[1].scity;
head.ecity:=path[1].ecity;
head.stime:=path[1].stime;
head.etime:=path[1].etime;
head.coday:=path[1].coday;
head.cotime:=path[1].cotime;
head.track:=path[1].track;
head.pascity:=path[1].pascity;
head.comoney:=path[1].comoney;
for h:=1 to 20 do head.arrpascity[h]:=0;
head.arrpascity[1]:=path[1].arrpascity[1];
head.arrpascity[2]:=path[1].arrpascity[2];
head.prior:=nil;
head.next:=t;
t.prior:=head;
j:=2;
while j<=个数 do
begin
t.no:=j;
t.scity:=path[j].scity;
t.ecity:=path[j].ecity;
t.stime:=path[j].stime;
t.etime:=path[j].etime;
t.coday:=path[j].coday;
t.track:=path[j].track;
t.cotime:=path[j].cotime;
t.pascity:=path[j].pascity;
t.comoney:=path[j].comoney;
for h:=1 to 20 do t.arrpascity[h]:=0;
t.arrpascity[1]:=path[j].arrpascity[1];
t.arrpascity[2]:=path[j].arrpascity[2];
m:=t;
new(t);
m.next:=t;
t.prior:=m;
j:=j+1;
end;
pathnum:=j-1;
til:=m;
til.next:=nil;
dispose(t);
end;procedure TForm1.readlink;
var
t:point;
j:integer;
begin
t:=head;
j:=1;
repeat
begin
if path[j].scity=0 then break;
path[j].scity:=t.scity;
path[j].ecity:=t.ecity;
path[j].stime:=t.stime;
path[j].etime:=t.etime;
path[j].coday:=t.coday;
path[j].cotime:=t.cotime;
path[j].track:=t.track;
path[j].pascity:=t.pascity;
path[j].comoney:=t.comoney;
t:=t.next;
j:=j+1;
end;
until t=til.next;
//dispose(t);
end;function TForm1.count(x:array of integer):integer;
var i,n:integer;
begin
n:=0;result:=0;
for i:=low(x) to high(x) do
if x[i]<>0 then n:=n+1 else break;
result:=n;
end;procedure TForm1.addlink;
var
x,p,q,r:point;
countp,countq,countnew,m,n,m1,n1:integer;
stop:boolean;begin
stop:=false;
for m1:=1 to 100 do for n1:=1 to 100 do judge[m1,n1]:=false; p:=til;
q:=til;
while p<>head.prior do
begin
while q<>head.prior do
begin
if (p<>q) and (p.ecity=q.scity) and (judge[p.no,q.no]=false) then //如果p<>q,且p的终点=q的起点并且没有被选择过
begin
new(r);
r.track:=p.track+','+q.track;
// showmessage(inttostr(p.no));
// showmessage(inttostr(q.no)); x:=head;
while x.next<>nil do
begin
if x.track=r.track then begin stop:=true;break;end;
x:=x.next;
end; if stop=true then begin q:=q.prior;continue;dispose(r);end; judge[p.no,q.no]:=true;
countp:=count(p.arrpascity);
countq:=count(q.arrpascity); for m:=1 to 20 do r.arrpascity[m]:=0;
for m:=1 to countp do
r.arrpascity[m]:=p.arrpascity[m];
n:=1;
for m:=countp to countq+countp-1 do
begin
r.arrpascity[m]:=q.arrpascity[n];
if n<countq then n:=n+1;
end; countnew:=count(r.arrpascity);
for m:=1 to countnew do //判断城市是否有重复
begin
for n:=1 to countnew do
begin
if m<>n then
if r.arrpascity[m]=r.arrpascity[n] then begin stop:=true;break;end;
end;
if stop=true then break;
end;
if stop=true then begin q:=q.prior;dispose(r);continue;end; r.pascity:=inttostr(r.arrpascity[1]); //把途径城市数组转成字符串
for m:=2 to countnew do
r.pascity:=r.pascity+','+inttostr(r.arrpascity[m]);
pathnum:=pathnum+1;
r.scity:=p.scity;
r.ecity:=q.ecity;
r.stime:=p.stime;
r.etime:=q.etime;
// showmessage(r.track);
r.comoney:=p.comoney+q.comoney;
r.no:=pathnum;
// showmessage(inttostr(r.no));
if p.etime<=q.stime then
begin
r.cotime:=p.cotime+q.cotime+q.stime-p.etime;
r.coday:=p.coday+q.coday-1;
end
else
begin
r.cotime:=p.cotime+q.cotime+q.stime-p.etime+24;
r.coday:=p.coday+q.coday;
end; til.next:=r;
r.prior:=til;
til:=r;
til.next:=nil;
til.prior:=til.prior; //测试 q:=til;
p:=til;
end;
q:=q.prior;
end;
q:=til;
p:=p.prior; end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
path[1].scity:=4;
path[1].ecity:=2;
path[1].stime:=3;
path[1].etime:=4;
path[1].coday:=1;
path[1].track:='1';
path[1].pascity:='4,2';
path[1].comoney:=200;
path[1].arrpascity[1]:=4;
path[1].arrpascity[2]:=2;
path[2].scity:=2;
path[2].ecity:=3;
path[2].stime:=4;
path[2].etime:=5;
path[2].coday:=1;
path[2].track:='2';
path[2].pascity:='2,3';
path[2].comoney:=200;
path[2].arrpascity[1]:=2;
path[2].arrpascity[2]:=3;
path[3].scity:=3;
path[3].ecity:=5;
path[3].stime:=4;
path[3].etime:=5;
path[3].coday:=1;
path[3].track:='3';
path[3].pascity:='3,5';
path[3].comoney:=200;
path[3].arrpascity[1]:=3;
path[3].arrpascity[2]:=5;
path[4].scity:=5;
path[4].ecity:=2;
path[4].stime:=4;
path[4].etime:=5;
path[4].coday:=1;
path[4].track:='4';
path[4].pascity:='5,2';
path[4].comoney:=200;
path[4].arrpascity[1]:=5;
path[4].arrpascity[2]:=2;end;procedure TForm1.Button1Click(Sender: TObject);
begin
buildlink;
addlink;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
readlink;
showmessage(inttostr(pathnum));
end;
info=record
arrpascity:array [1..20] of integer;
no,scity,ecity,stime,etime,coday,cotime,comoney:integer;
track,pascity:string;
next,prior:point;
end;
info1=record
arrpascity:array [1..20] of integer;
scity,ecity,stime,etime,coday,cotime,comoney:integer;
track,pascity:string;
end;var
Form1: TForm1;
head,til:point;
pathnum:integer;
path:array [1..10000] of info1;
judge:array [1..10000,1..10000] of boolean;
implementation
{$R *.dfm}procedure TForm1.buildlink;
var
j,h:integer;
m,t:point;
begin
new(head);
new(t);
head.no:=1;
head.scity:=path[1].scity;
head.ecity:=path[1].ecity;
head.stime:=path[1].stime;
head.etime:=path[1].etime;
head.coday:=path[1].coday;
head.cotime:=path[1].cotime;
head.track:=path[1].track;
head.pascity:=path[1].pascity;
head.comoney:=path[1].comoney;
for h:=1 to 20 do head.arrpascity[h]:=0;
head.arrpascity[1]:=path[1].arrpascity[1];
head.arrpascity[2]:=path[1].arrpascity[2];
head.prior:=nil;
head.next:=t;
t.prior:=head;
j:=2;
while j<=个数 do
begin
t.no:=j;
t.scity:=path[j].scity;
t.ecity:=path[j].ecity;
t.stime:=path[j].stime;
t.etime:=path[j].etime;
t.coday:=path[j].coday;
t.track:=path[j].track;
t.cotime:=path[j].cotime;
t.pascity:=path[j].pascity;
t.comoney:=path[j].comoney;
for h:=1 to 20 do t.arrpascity[h]:=0;
t.arrpascity[1]:=path[j].arrpascity[1];
t.arrpascity[2]:=path[j].arrpascity[2];
m:=t;
new(t);
m.next:=t;
t.prior:=m;
j:=j+1;
end;
pathnum:=j-1;
til:=m;
til.next:=nil;
dispose(t);
end;procedure TForm1.readlink;
var
t:point;
j:integer;
begin
t:=head;
j:=1;
repeat
begin
if path[j].scity=0 then break;
path[j].scity:=t.scity;
path[j].ecity:=t.ecity;
path[j].stime:=t.stime;
path[j].etime:=t.etime;
path[j].coday:=t.coday;
path[j].cotime:=t.cotime;
path[j].track:=t.track;
path[j].pascity:=t.pascity;
path[j].comoney:=t.comoney;
t:=t.next;
j:=j+1;
end;
until t=til.next;
//dispose(t);
end;function TForm1.count(x:array of integer):integer;
var i,n:integer;
begin
n:=0;result:=0;
for i:=low(x) to high(x) do
if x[i]<>0 then n:=n+1 else break;
result:=n;
end;procedure TForm1.addlink;
var
x,p,q,r:point;
countp,countq,countnew,m,n,m1,n1:integer;
stop:boolean;begin
stop:=false;
for m1:=1 to 100 do for n1:=1 to 100 do judge[m1,n1]:=false; p:=til;
q:=til;
while p<>head.prior do
begin
while q<>head.prior do
begin
if (p<>q) and (p.ecity=q.scity) and (judge[p.no,q.no]=false) then //如果p<>q,且p的终点=q的起点并且没有被选择过
begin
new(r);
r.track:=p.track+','+q.track;
// showmessage(inttostr(p.no));
// showmessage(inttostr(q.no)); x:=head;
while x.next<>nil do
begin
if x.track=r.track then begin stop:=true;break;end;
x:=x.next;
end; if stop=true then begin q:=q.prior;continue;dispose(r);end; judge[p.no,q.no]:=true;
countp:=count(p.arrpascity);
countq:=count(q.arrpascity); for m:=1 to 20 do r.arrpascity[m]:=0;
for m:=1 to countp do
r.arrpascity[m]:=p.arrpascity[m];
n:=1;
for m:=countp to countq+countp-1 do
begin
r.arrpascity[m]:=q.arrpascity[n];
if n<countq then n:=n+1;
end; countnew:=count(r.arrpascity);
for m:=1 to countnew do //判断城市是否有重复
begin
for n:=1 to countnew do
begin
if m<>n then
if r.arrpascity[m]=r.arrpascity[n] then begin stop:=true;break;end;
end;
if stop=true then break;
end;
if stop=true then begin q:=q.prior;dispose(r);continue;end; r.pascity:=inttostr(r.arrpascity[1]); //把途径城市数组转成字符串
for m:=2 to countnew do
r.pascity:=r.pascity+','+inttostr(r.arrpascity[m]);
pathnum:=pathnum+1;
r.scity:=p.scity;
r.ecity:=q.ecity;
r.stime:=p.stime;
r.etime:=q.etime;
// showmessage(r.track);
r.comoney:=p.comoney+q.comoney;
r.no:=pathnum;
// showmessage(inttostr(r.no));
if p.etime<=q.stime then
begin
r.cotime:=p.cotime+q.cotime+q.stime-p.etime;
r.coday:=p.coday+q.coday-1;
end
else
begin
r.cotime:=p.cotime+q.cotime+q.stime-p.etime+24;
r.coday:=p.coday+q.coday;
end; til.next:=r;
r.prior:=til;
til:=r;
til.next:=nil;
til.prior:=til.prior; //测试 q:=til;
p:=til;
end;
q:=q.prior;
end;
q:=til;
p:=p.prior; end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
path[1].scity:=4;
path[1].ecity:=2;
path[1].stime:=3;
path[1].etime:=4;
path[1].coday:=1;
path[1].track:='1';
path[1].pascity:='4,2';
path[1].comoney:=200;
path[1].arrpascity[1]:=4;
path[1].arrpascity[2]:=2;
path[2].scity:=2;
path[2].ecity:=3;
path[2].stime:=4;
path[2].etime:=5;
path[2].coday:=1;
path[2].track:='2';
path[2].pascity:='2,3';
path[2].comoney:=200;
path[2].arrpascity[1]:=2;
path[2].arrpascity[2]:=3;
path[3].scity:=3;
path[3].ecity:=5;
path[3].stime:=4;
path[3].etime:=5;
path[3].coday:=1;
path[3].track:='3';
path[3].pascity:='3,5';
path[3].comoney:=200;
path[3].arrpascity[1]:=3;
path[3].arrpascity[2]:=5;
path[4].scity:=5;
path[4].ecity:=2;
path[4].stime:=4;
path[4].etime:=5;
path[4].coday:=1;
path[4].track:='4';
path[4].pascity:='5,2';
path[4].comoney:=200;
path[4].arrpascity[1]:=5;
path[4].arrpascity[2]:=2;end;procedure TForm1.Button1Click(Sender: TObject);
begin
buildlink;
addlink;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
readlink;
showmessage(inttostr(pathnum));
end;
arrpascity表示途径城市
scity,ecity,stime,coday,cotime,comoney分别表示起点、终点、起始时刻、终点时刻、天数、用时、钱数
track是经过路径的字符串
pascity是经过城市的字符串,和arrpascity数组转换取得我用的算法就是把预存进数组的信息建成链表后,p和q两个指针从后向前寻找满足p.b=q.a的项,若找到,则生成一项t.a=p.a,t.b=q.b,并存入其他信息后,把p,q重新遍历,因为我事先定义了一个二元布尔数组,判断x,y是否已经比较过,请大家帮我看看问题到底在哪里