-- File graphtrv.adb -- This is a program that instantiates and populates a graph, in -- response to commands input from a data file. Some of those -- commands require either a depth-first or breadth-first traversal of -- the graph. The program uses both a stack and a queue as auxiliary -- data structures to implement those traversals. with text_io, stackg, queueg, graphg; use text_io; procedure graphtrv is package my_graph is new graphg (integer); use my_graph; Package IntIO is new Integer_IO (integer); use IntIO; package graph_stack is new stackg (Iterator); use graph_stack; package graph_queue is new queueg (Iterator); use graph_queue; IntGraph: Graph (25); InData, OutData: File_Type; Cmd: Character; procedure AddNode (InGraph: in out Graph) is number, value: integer; begin Get (InData, number); Get (InData, value); Add (number, value, InGraph); Put (OutData, " Added node number "); Put (OutData, number); Put (OutData, " with value "); Put (OutData, value); New_Line (OutData); end AddNode; procedure AddEdge (InGraph: in out Graph) is from, to: integer; begin Get (InData, from); Get (InData, to); InsertEdge (from, to, InGraph); Put (OutData, " Added an edge from node "); Put (OutData, from); Put (OutData, " to node "); Put (OutData, to); New_Line (OutData); end AddEdge; procedure Breadth (OfGraph: in out Graph) is IStack: Stack; start: integer; curnode, temp: Iterator; begin Clear (IStack); Get (InData, start); Put (OutData, " Starting breadth-first traversal from node "); Put (OutData, start); New_Line (OutData); Initialize (curnode, OfGraph, start); Push (curnode, IStack); while not Empty (IStack) loop Put ("Halt - press Return"); Skip_Line; curnode := Top (IStack); Pop (IStack); Put (OutData, " Visit node with value "); Put (OutData, ValueOf (curnode, OfGraph)); New_Line (OutData); temp := curnode; while not Done (curnode, OfGraph) loop GetNext (temp, OfGraph); Push (temp, IStack); end loop; end loop; end Breadth; procedure Depth (OfGraph: in out Graph) is IQueue: Queue; start: integer; curnode, temp: Iterator; begin Clear (IQueue); Get (InData, start); Put (OutData, " Starting depth-first traversal from node "); Put (OutData, start); New_Line (OutData); Initialize (curnode, OfGraph, start); Add (curnode, IQueue); while not Empty (IQueue) loop Put ("Halt - press Return"); Skip_Line; curnode := First (IQueue); Pop (IQueue); Put (OutData, " Visit node with value "); Put (OutData, ValueOf (curnode, OfGraph)); New_Line (OutData); temp := curnode; while not Done (curnode, OfGraph) loop GetNext (temp, OfGraph); Add (temp, IQueue); end loop; end loop; end Depth; begin open (InData, IN_FILE, "graphtrv.dat"); open (OutData, OUT_FILE, "graphtrv.out"); Clear (IntGraph); Put (OutData, "*** Beginning to process command data file ***"); New_Line (OutData); while not End_Of_File (InData) loop get (InData, Cmd); case Cmd is when 'N' => AddNode (IntGraph); when 'E' => AddEdge (IntGraph); when 'B' => Breadth (IntGraph); when 'D' => Depth (IntGraph); when others => null; end case; Skip_Line (InData); end loop; Put (OutData, "*** Done processing command data file ***"); New_Line (OutData); end graphtrv;