comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: Simple Pointer Problem, Help...
Date: 1996/05/24
Date: 1996-05-24T00:00:00+00:00	[thread overview]
Message-ID: <4o4jf6$akv@newsbf02.news.aol.com> (raw)
In-Reply-To: 4o2vu9$hdc@catapult.gatech.edu


gt7693d@acmex.gatech.edu (Daniel J) had
trouble debugging a linked list program.
     Your procedure to add a link to the end of the linked list,
AddNodeBack, finds the end of the linked list correctly, but fails to
store the pointer to the new node in <last node>.Next.  You need to keep a
pointer to the head of the linked list in a variable (e.g., Num) that
doesn't get updated while you grow the list, and you need to pass that
*head* of the linked list to AddNodeBack.  Your procedure should have a
local variable (e.g., Temp) to iterate through the list and find the end,
and another local, (SaveTemp) to keep track of the last node so you can
set <last note>.Next.  Here's your corrected procedure:

     --Purpose: Add a node to the end of a linked list
     --I changed Point to Head and added Temp and SaveTemp.
     procedure AddNodeBack (Head  : in out NumPtr;
                            Value : in Integer) is
       Temp     : NumPtr := Head;
       SaveTemp : NumPtr;
     begin
       if Head /= null then
         while Temp /= null loop
           SaveTemp := Temp;
           Temp     := Temp.Next;
         end loop;
         Temp          := new NumRecord'(Value, null);
         SaveTemp.Next := Temp;
       else
         Head := new NumRecord'(Value, null);
       end if;
    
     end AddNodeBack;

     The procedure could be made simpler by eliminating the test for Head
/= null, if you're willing to add an unused link to the beginning of the
linked list.  My Ada Tutor program, available at the WWW and FTP sites
below my signature, gives an example.  The example is a program that gets
integers in any order from the terminal, maintaining a linked list of them
in ascending order.  When the user types 0, the programs outputs the
integers in ascending order.  The procedure to add a node is very simple.
     I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




      parent reply	other threads:[~1996-05-24  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-24  0:00 Simple Pointer Problem, Help Daniel J
1996-05-24  0:00 ` Peter Hermann
1996-05-24  0:00 ` John Herro [this message]
replies disabled

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