with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with Unchecked_Conversion;
procedure topologicalSort is
--create linked list nodes for 2 dimensional array to hold pointers
numberOfActions: integer :=0;
Kn: integer :=0;
j: integer :=0;
k: integer :=0;
r: integer :=0;
f: integer :=0;
type sNode;
type sLink is access sNode;
type sNode is
record
succ: integer;
next: sLink;
end record;
function sLinkToInt is new Unchecked_Conversion(sLink, integer);
function intTosLink is new Unchecked_Conversion(integer, sLink);
p: sLink;
function getSize return integer is
na: integer;
begin
put_line("+++++++++++++++++++++++++++++++++++++++++++++++");
put_line("+++ Welcome to the Topological Sort Program +++ ");
put_line("+++++++++++++++++++++++++++++++++++++++++++++++");
put("Please enter the number of actions ::");
ada.integer_text_io.get(na);
return na;
end getSize;
begin
numberofActions:=getSize;
Kn:=numberOfActions;
declare
count: array (1 .. numberOfActions) of integer;
top: array (1 .. numberOfActions) of sLink;
qlink: array (0 .. numberOfActions) of integer;
begin
--Step1
for k in 1 .. numberOfActions loop
count(k):=0;
top(k):=null;
end loop;
--Step2
put_line("Please enter relations one at a time:::::::::");
put_line("Input in 'j k' fashion where j preceeds k:::::");
put_line("to end the sequence, enter '1337'::::::::::::");
loop
get(j);
exit when j = 1337;
get(k);
count(k):= count(k)+1;
p:=new sNode'(k,top(j));
top(j):=p;
end loop;
--Step3
put("step3");
r:=0;
qlink(0):=0;
for k in 1 .. numberOfActions loop
if count(k)=0 then
qlink(r):=k;
r:=k;
end if;
end loop;
f:=qlink(0);
--Step4
put("step4");
while f /=0 loop
put(f);
kn:=kn-1;
p:=top(f);
top(f):=intTosLink(0);
while p/=null and p /= intTosLink(0) loop
count(p.succ) := count(p.succ)-1;
if count(p.succ) = 0 then
qlink(r):=p.succ;
r:=p.succ;
end if;
p:=p.next;
end loop;
put(qlink(f)); --this is used to debug and has been identified as why top(f) gets the error raised CONSTRAINT_ERROR : topologicalsort.adb:101 index check failed
f:=qlink(f);
end loop;
--Step5
put("step5");
if kn = 0 then
put("Successful topological sort");
else
put("Failed topological sort, now printing error::");
for k in 1 .. numberOfActions loop
qlink(k):=0;
end loop;
end if;
--Step6
for k in 1 .. numberOfActions loop
p:=top(k);
top(k):=intTosLink(0);
while p /= null and p /=intTosLink(0) and qlink(p.succ) = 0 loop --raised CONSTRAINT_ERROR : topologicalsort.adb:134 access check failed
qlink(p.succ):=k;
if p /= intTosLink(0) then
p:=p.next;
end if;
end loop;
end loop;
--Step7
k:=1;
while(qlink(k) = 0) loop
k:= k + 1;
end loop;
--Step8
loop
top(k):=intTosLink(1); k:=qlink(k);
exit when top(k) /= intTosLink(0);
end loop;
--Step9
while top(k) /= intTosLink(0) loop
ada.integer_text_io.put(k);
top(k):=intTosLink(0);
k:=qlink(k);
end loop;
end;
end topologicalSort;