home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch23_1.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.9 KB  |  106 lines

  1.                         -- Chapter 23 - Programming exercise 1
  2. with Text_IO, Unchecked_Deallocation;
  3. use Text_IO;
  4.  
  5. procedure CH23_1 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    Data_String : constant STRING := "This tests ADA";
  11.  
  12.    type CHAR_REC;                 -- Incomplete declaration
  13.  
  14.    type CHAR_REC_POINT is access CHAR_REC;
  15.  
  16.    type CHAR_REC is               -- Complete declaration
  17.       record
  18.          One_Letter : CHARACTER;
  19.          Next_Rec   : CHAR_REC_POINT;
  20.       end record;
  21.  
  22.    Start : CHAR_REC_POINT;        -- Always points to start of list
  23.    Last  : CHAR_REC_POINT;        -- Points to the end of the list
  24.  
  25.    procedure Free is new
  26.                    Unchecked_Deallocation(CHAR_REC,CHAR_REC_POINT);
  27.  
  28.    procedure Traverse_List(Starting_Point : CHAR_REC_POINT) is
  29.    Temp : CHAR_REC_POINT;         -- Moves through the list
  30.    begin
  31.       Put("In traverse routine.  --->");
  32.       Temp := Starting_Point;
  33.       if Temp = null then
  34.          Put("No data in list.");
  35.       else
  36.          loop
  37.             Put(Temp.One_Letter);
  38.             Temp := Temp.Next_Rec;
  39.             if Temp = null then exit; end if;
  40.          end loop;
  41.       end if;
  42.       New_Line;
  43.    end Traverse_List;
  44.  
  45.    procedure Store_Character(In_Char : CHARACTER) is
  46.    Temp : CHAR_REC_POINT;
  47.    begin
  48.       Temp := new CHAR_REC;
  49.       Temp.One_Letter := In_Char;    -- New record is now defined
  50.                                      -- The system sets Next_Rec
  51.                                      -- to the value of null
  52.       if Start = null then
  53.          Start := Temp;
  54.          Last := Temp;
  55.       else
  56.          Last.Next_Rec := Temp;
  57.          Last := Temp;
  58.       end if;
  59.       Traverse_List(Start);
  60.    end Store_Character;
  61.  
  62. begin
  63.             -- Store the characters in Data_String in a linked list
  64.    for Index in Data_String'RANGE loop
  65.       Store_Character(Data_String(Index));
  66.    end loop;
  67.  
  68.             -- Traverse the final list
  69.    New_Line;
  70.    Put_Line("Now for the final traversal.");
  71.    Traverse_List(Start);
  72.  
  73.             -- Free the entire list now
  74.    loop
  75.       exit when Start = null;
  76.       Last := Start.Next_Rec;
  77.       Free(Start);
  78.       Start := Last;
  79.    end loop;
  80.  
  81. end CH23_1;
  82.  
  83.  
  84.  
  85.  
  86. -- Result of execution
  87.  
  88. -- In traverse routine.  --->T
  89. -- In traverse routine.  --->Th
  90. -- In traverse routine.  --->Thi
  91. -- In traverse routine.  --->This
  92. -- In traverse routine.  --->This
  93. -- In traverse routine.  --->This t
  94. -- In traverse routine.  --->This te
  95. -- In traverse routine.  --->This tes
  96. -- In traverse routine.  --->This test
  97. -- In traverse routine.  --->This tests
  98. -- In traverse routine.  --->This tests
  99. -- In traverse routine.  --->This tests A
  100. -- In traverse routine.  --->This tests AD
  101. -- In traverse routine.  --->This tests ADA
  102. --
  103. -- Now for the final traversal.
  104. -- In traverse routine.  --->This tests ADA
  105.  
  106.