comp.lang.ada
 help / color / mirror / Atom feed
* Simple Pointer Problem, Help...
@ 1996-05-24  0:00 Daniel J
  1996-05-24  0:00 ` Peter Hermann
  1996-05-24  0:00 ` John Herro
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel J @ 1996-05-24  0:00 UTC (permalink / raw)




To any friendly programmer,

	Take a look at this code and read the comments at end, they seem
	self explanatory.


--Types and declarations

  type NumRecord;
  
  type NumPtr is access NumRecord;

  type NumRecord is
     record
        Value : Integer;
        Next  : NumPtr;
     end record;

  Num : NumPtr;



--Modules

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



--Body
     
     Num := new NumRecord'(0, null);

     for I in 1..10 loop
        AddNodeBack (Num, I);
     end loop;



	The output of the body gives me one node, which is not what I want.
I desire a linked list of N nodes w/values in ascending order, according to
my body.  

     What am I doing wrong, for I've run outta ideas?

ps I've tried to use a temp pointer within AddNodeBack, but got the same
   output.



_____________________________________________________________________________
     Daniel J. Feren              |                
     327693 GA Tech Station       |              GTRI/ELSYS/SEN
     Atlanta, Georgia 30332       |               Co-Op Win/Sum
__________________________________|__________________________________________
		   Email: gt7693d@prism.gatech.edu
	    Homepage: http://www.prism.gatech.edu/~gt7693d




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

* Re: Simple Pointer Problem, Help...
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Hermann @ 1996-05-24  0:00 UTC (permalink / raw)



Daniel J (gt7693d@acmex.gatech.edu) wrote:

>To any friendly programmer,

this sounds like "to a friendly Ada-Teamer" ;-)
so I give it a try :-)

> procedure AddNodeBack (Point : in out NumPtr;
>                          Value : in Integer) is
         p : numptr := point;
>     begin
        if p=null then
           Point := new NumRecord'(Value, null);
        else
           while  P.next /= null loop
             P := P.Next;    end loop;
                  P.next := new NumRecord'(Value, null);
        end if;
>    
>     end AddNodeBack;

>	The output of the body gives me one node, which is not what I want.

true ;-)

>I desire a linked list of N nodes w/values in ascending order, according to

you have indeed produced a linked list but you can't reach it  ;-)
The original pointer is ultimately destroyed in your original code :-(
Caution: the above piece of suggested code is not tested,
i.e. it is certainly wrong... as it is usual for the first trial ;-)
Daniel, it is your turn now to test and give me the correct solution
with all mumble-dee-bumble around
(I need a lot of working examples for my text-booklet).

--
Peter Hermann  Tel:+49-711-685-3611 Fax:3758 ph@csv.ica.uni-stuttgart.de
Pfaffenwaldring 27, 70569 Stuttgart Uni Computeranwendungen
Team Ada: "C'mon people let the world begin" (Paul McCartney)




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

* Re: Simple Pointer Problem, Help...
  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
  1 sibling, 0 replies; 3+ messages in thread
From: John Herro @ 1996-05-24  0:00 UTC (permalink / raw)



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




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

end of thread, other threads:[~1996-05-24  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox