Witam, mam problem. Próbowałem stworzyć liste podwieszaną, jednak umiałem tylko tworzyć liste jednokierunkową... Lista podwieszana ma wyglądać tak:
http://i.imgur.com/E8httYZ.png
Oto 3 procedury(pierwsza to tworzenie listy jednokierunkowej, druga znajduje żądany węzeł listy, a trzecia dodaje podliste do znalezionego węzła)
Czy kod będzie poprawny?
Typy:
type PEL=^EL;
EL = record
d : integer;
next: PEL;
list: PEL; // the pointer to another list
end;
Procedury:
procedure add(var h:PEL; d: integer);
var
new_ele: PEL;
begin
new(new_ele);
new_ele^.d:=d;
new_ele^.next:=h;
h:=new_ele;
end;
Oraz dwie procedury, co do których nie jestem pewny...:
function found_el(var h:PEL,d:integer):PEL
var t:PEL
begin
t:=h
write('Please enter the key of the node that you want to add sublist')
readln(d);
while d<>t^.d do
t:=t^.next
h:=found_el
end;
procedure addsublist(var head_sublist:PEL; d: integer);
var
new_ele,t: PEL;
begin
new(new_ele);
new_ele^.d:=d;
new_ele^.next:=nil;
t:=found_el^.list;
if t=nil then
t:=head_sublist;
while head_sublist^.next <> nil do
head_sublist:=head_sublist^.next;
head_sublist^.next:=new_ele;
end;
Jeżeli jest dobry, to można by było jakos bardziej zoptymalizować; 'skrócić' ?