comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: chained List manipulation without recusrsion : lost the begining of my List!
Date: Thu, 15 May 2003 19:02:56 GMT
Date: 2003-05-15T19:02:56+00:00	[thread overview]
Message-ID: <3EC3E50D.1000004@spam.com> (raw)
In-Reply-To: ba040h$nb5$1@aphrodite.grec.isp.9tel.net

Zeg wrote:
> I missed inserting at the head of the List so this the
> 
>  ultimate code

I've reformatted it to help me understand it, used consistent 
capitalization, added some blank lines and removed others, and removed 
unnecessary parentheses and ".all"s. This was to help me more than 
anything, but good formatting and consistent capitalization are 
generally a Good Thing. It's usually not a Good Idea to use tabs for 
indentation; 2-4 blanks is usually good.

> 
> procedure Insert_iteratif (E : in Element; L : in out Liste) is
>    Aux : Liste := L;
>    Z   : Liste;
> begin

Do you check your precondition (L is sorted) anywhere?

>    if L = null then
>       L := new Maillon'(E, null);
>    else
>       while Aux /= null loop
>          if Aux.Valeur > E then
>             if Z = null then
>                L := new Maillon'(E, L);
>             else
>                Z.Suivant := new Maillon'(E, Aux);
>             end if;
 >
>             exit;
>          elsif Aux.suivant = null then
>             Z.Suivant := new Maillon'(E, null);

At the end of the list. It seems to me that it is possible for Z to be 
null here, for example if L has exactly 1 element.

 >
>             exit;
>          end if;
 >
>          Z := Aux;
>          Aux := Aux.Suivant;
>       end loop;
>    end if;
> end Insert_iteratif;

It seems you've missed the case of adding to the end of a single-element 
list.

It would be clearer what you're doing if you used names such as 
"Current" and "Previous" rather than "Aux" and "Z". (You might prefer 
French equivalents.)

You don't need to put parentheses around conditions, the way you have to 
in some languages. Nor do you have to put ".all" when referencing a 
component of something designated by an access value. This is part of 
the idea of allowing something to change from statically allocated to 
dynamically allocated (or from dynamic to static) without having to add 
or remove a bunch of ".all"s.

-- 
Jeff Carter
"I'm a lumberjack and I'm OK."
Monty Python's Flying Circus




  reply	other threads:[~2003-05-15 19:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-14 23:20 chained List manipulation without recusrsion : lost the begining of my List! Zeg
2003-05-15  5:58 ` Simon Wright
2003-05-15 12:25   ` Zeg
2003-05-15 13:16   ` Zeg
2003-05-15 19:02     ` Jeffrey Carter [this message]
2003-05-16  1:11   ` Robert A Duff
2003-05-17  7:03     ` Simon Wright
replies disabled

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