with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
procedure Main is
   type Node;
   type List is access Node;
   type Node is record
      Value : Integer;
      Next : List;
   end record;
   procedure Free is new Ada.Unchecked_Deallocation (Node, List);

   --  through an access parameter: the caller's node, changed in place
   procedure Set (N : not null access Node; V : Integer) is
   begin
      N.Value := V;
   end Set;

   --  a traversal function: an observer of the node after the first
   function Second (L : access constant Node) return access constant Node is
     (if L = null then null else L.Next);

   L : List := new Node'(1, new Node'(2, null));
begin
   Set (L, 10);
   declare
      S : constant access constant Node := Second (L);
   begin
      if S /= null then
         Ada.Text_IO.Put_Line (Integer'Image (S.Value));
      end if;
   end;
   Ada.Text_IO.Put_Line (Integer'Image (L.Value));
   while L /= null loop
      declare
         Rest : List := L.Next;
      begin
         Free (L);
         L := Rest;
      end;
   end loop;
end Main;
