comp.lang.ada
 help / color / mirror / Atom feed
* Why does that work?
@ 2015-03-03 22:36 Laurent
  2015-03-04  5:51 ` Laurent
  0 siblings, 1 reply; 15+ messages in thread
From: Laurent @ 2015-03-03 22:36 UTC (permalink / raw)


Hi

Why does It.L.Head or Tail work?

It is of type Iterator defined as:

   type Iterator (L : access List) is
      record
         This :  List_Ptr:= null;
      end record;

If It points to the first node in the List L then It.This.Previous = null
and if It points to the last node in the List L then It.This.Next = null.

The pointers are working only in one direction I think? So if I am the pointer I will never see the head or the tail of the list. Probably a too simple/naive view. 

So how can It "know" something about the Head or Tail of the List? Is that caused by L: access List?

In my test program all the nodes are known at compile time. Didn't have the time to write a test to see what happens if I add one node to the list at runtime. Wouldn't that somehow cause a runtime error or simply not work as intended?

Just a small example:

   procedure Add_To_End (L : access List; Element : in Element_Type) is
      It : Iterator (L);

      begin -- Add_To_End
      
         if It.L.Head = null then -- List L is empty
            It.L.Head := new List_Node'(Element, null, null);
            It.L.Tail := It.L.Head;

         else
            It.L.Tail.Next := new List_Node'(Element, null, It.L.Tail);
            It.L.Tail := It.L.Tail.Next;
         end if;
      end Add_To_End;

Hm the first time I whine about something working and I didn't expect it :)

Thanks

Laurent


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2015-03-06 21:17 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-03 22:36 Why does that work? Laurent
2015-03-04  5:51 ` Laurent
2015-03-04 15:46   ` David Botton
2015-03-04 17:35     ` Simon Wright
2015-03-04 18:38       ` Laurent
2015-03-05 19:13         ` jan.de.kruyf
2015-03-05 21:16           ` Laurent
2015-03-06  0:51             ` Ada Video Course David Botton
2015-03-06 15:42               ` Jacob Sparre Andersen
2015-03-06 17:01                 ` G.B.
2015-03-06 21:17                 ` David Botton
2015-03-06  6:20             ` Why does that work? jan.de.kruyf
2015-03-06 13:28               ` Laurent
2015-03-06 14:07                 ` G.B.
2015-03-06 14:24                   ` jan.de.kruyf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox