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

  1.                                        -- Chapter 23 - Program 1
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure LinkList 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 Traverse_List(Starting_Point : CHAR_REC_POINT) is
  26.    Temp : CHAR_REC_POINT;         -- Moves through the list
  27.    begin
  28.       Put("In traverse routine.  --->");
  29.       Temp := Starting_Point;
  30.       if Temp = null then
  31.          Put("No data in list.");
  32.       else
  33.          loop
  34.             Put(Temp.One_Letter);
  35.             Temp := Temp.Next_Rec;
  36.             if Temp = null then exit; end if;
  37.          end loop;
  38.       end if;
  39.       New_Line;
  40.    end Traverse_List;
  41.  
  42.    procedure Store_Character(In_Char : CHARACTER) is
  43.    Temp : CHAR_REC_POINT;
  44.    begin
  45.       Temp := new CHAR_REC;
  46.       Temp.One_Letter := In_Char;    -- New record is now defined
  47.                                      -- The system sets Next_Rec
  48.                                      -- to the value of null
  49.       if Start = null then
  50.          Start := Temp;
  51.          Last := Temp;
  52.       else
  53.          Last.Next_Rec := Temp;
  54.          Last := Temp;
  55.       end if;
  56.       Traverse_List(Start);
  57.    end Store_Character;
  58.  
  59. begin
  60.             -- Store the characters in Data_String in a linked list
  61.    for Index in Data_String'RANGE loop
  62.       Store_Character(Data_String(Index));
  63.    end loop;
  64.  
  65.             -- Traverse the final list
  66.    New_Line;
  67.    Put_Line("Now for the final traversal.");
  68.    Traverse_List(Start);
  69.  
  70.             -- Free the entire list now
  71.    loop
  72.       exit when Start = null;
  73.       Last := Start.Next_Rec;
  74.       Start.Next_Rec := null;
  75.       Start := Last;
  76.    end loop;
  77.  
  78. end LinkList;
  79.  
  80.  
  81.  
  82.  
  83. -- Result of execution
  84.  
  85. -- In traverse routine.  --->T
  86. -- In traverse routine.  --->Th
  87. -- In traverse routine.  --->Thi
  88. -- In traverse routine.  --->This
  89. -- In traverse routine.  --->This
  90. -- In traverse routine.  --->This t
  91. -- In traverse routine.  --->This te
  92. -- In traverse routine.  --->This tes
  93. -- In traverse routine.  --->This test
  94. -- In traverse routine.  --->This tests
  95. -- In traverse routine.  --->This tests
  96. -- In traverse routine.  --->This tests A
  97. -- In traverse routine.  --->This tests AD
  98. -- In traverse routine.  --->This tests ADA
  99. --
  100. -- Now for the final traversal.
  101. -- In traverse routine.  --->This tests ADA
  102.  
  103.