comp.lang.ada
 help / color / mirror / Atom feed
* Code Confusion.
@ 1997-11-02  0:00 Supreme
  1997-11-02  0:00 ` bklungle
  1997-11-04  0:00 ` Bill
  0 siblings, 2 replies; 3+ messages in thread
From: Supreme @ 1997-11-02  0:00 UTC (permalink / raw)



In the following program I am trying to display the values of linked
list instead my output is
80 80 80 80 80 80
instead of 40 50 60 70 80.
here it is.
procedure problem is
    type LIST_NODE;
    type NODE_PTR is access LIST_NODE;
    type LIST_NODE is record
                NUM : INTEGER;
                NEXT : NODE_PTR;
     end record;

HEAD,P  : NODE_PTR;
N, NUM_NODES : INTEGER;
begin -- lab10A
    Put("Please enter the the values of the nodes");
Get( Num_Nodes);
Head := new NODE;
New_Line;
P := Head;
    for K in 2..NUN_NODES loop
Head.Next := P;
P.ALL.NUM := N;
P := P.Next;
end loop;
    Will this allow teh user to assing 20 30 40 50 etc to the linked
list?
in other words
Head.Next := P Should point the Head.next to the Pointer P and
then P.ALL.NUM :=N should be set to the value of N  ( entered by the
user)
then P := P.Next should allow N to be assigned to the next pointer.
is this right??
Peace.





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

* Re: Code Confusion.
  1997-11-02  0:00 Code Confusion Supreme
@ 1997-11-02  0:00 ` bklungle
  1997-11-04  0:00 ` Bill
  1 sibling, 0 replies; 3+ messages in thread
From: bklungle @ 1997-11-02  0:00 UTC (permalink / raw)



At a quick glance, it looks like your code is generating a single node loop
on itself (among other things.)
Using your constructs, one possible sequence for building a push-down stack
linked list of nodes could be as follows:
..
..
begin  --lab10A
  -- assuming a use clause was given for text_io (or equivalent)
  put("Please enter the number of nodes desired ");
  -- assuming a new and use clause was given for integer_io (or equivalent)
  get(NUM_NODES);  -- get integer number of nodes
  skip_line;                   -- make sure no garbage at end of line
  if NUM_NODES /= 0 then
    HEAD := new LIST_NODE;
    put("Please enter integer node value for node 1: ");
    get(HEAD.NUM);    -- get nodes integer value
    skip_line;                 -- make sure no garbage at end of line
    for i in 2 .. NUM_NODES loop
      P := new LIST_NODE;  -- get another dynamic node
      P.NEXT := HEAD;           -- link current head behind this one
      HEAD    := P;                   -- this is now the current head
      put("Please enter integer node value for node " & integer'image(i) & "
");
      get(P.Num);
      skip_line;              -- make sure no garbage at end of line
    end loop;
    .
    .
  end if;

  -- Note: This is not the best, or only way to do this, only one quick way.

Cheers...bob

Supreme wrote in message <345CD154.1249E04E@eng.ua.edu>...
>In the following program I am trying to display the values of linked
>list instead my output is
>80 80 80 80 80 80
>instead of 40 50 60 70 80.
>here it is.
>procedure problem is
>    type LIST_NODE;
>    type NODE_PTR is access LIST_NODE;
>    type LIST_NODE is record
>                NUM : INTEGER;
>                NEXT : NODE_PTR;
>     end record;
>
>HEAD,P  : NODE_PTR;
>N, NUM_NODES : INTEGER;
>begin -- lab10A
>    Put("Please enter the the values of the nodes");
>Get( Num_Nodes);
>Head := new NODE;
>New_Line;
>P := Head;
>    for K in 2..NUN_NODES loop
>Head.Next := P;
>P.ALL.NUM := N;
>P := P.Next;
>end loop;
>    Will this allow teh user to assing 20 30 40 50 etc to the linked
>list?
>in other words
>Head.Next := P Should point the Head.next to the Pointer P and
>then P.ALL.NUM :=N should be set to the value of N  ( entered by the
>user)
>then P := P.Next should allow N to be assigned to the next pointer.
>is this right??
>Peace.
>






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

* Re: Code Confusion.
  1997-11-02  0:00 Code Confusion Supreme
  1997-11-02  0:00 ` bklungle
@ 1997-11-04  0:00 ` Bill
  1 sibling, 0 replies; 3+ messages in thread
From: Bill @ 1997-11-04  0:00 UTC (permalink / raw)



 Your code only creates one node, stores a value, then links it to itself
Num_Nodes times. That is why they all have the same integer stored in the
record!
 First, the user input code would work better as a seperate proceedure that
passes the integer you want to store in the record.
The value would be passed to a 2nd proceedure that stores nodes in the
linked list.  Make sure you have the store proceedure handle all 4 possible
cases (ie, Store to an empty list, store to the front of the list, store at
the back of the list, store somewhere in the middle). The algorithm you
write will sort by the integer value going in since, being the only
non-access value in the record, it has to be your key.You could create new
nodes like this: (at the front or to an Empty List)

Head: NEW Node(I, Head);

This is a qualified aggregate.
I used I as the in parameter. Remember that the right side of the assignment
statement is always evaluated first in Ada.
 This could be done with local variables when you are searching where to put
the node in the list. That is , assign the new node(with Next set to NULL)to
a temp variable. Use 2 more to keep track of where you are as you search
through the list.  Something like this: (R is a record: just pretend it is
whatever you are passing in).

 Temp:= NEW Node'(R, NULL);
       Previous:= L.Front;
       Current:= L.Front.Next;
       While Previous /= NULL and then
                     R.Student_ID_Number >= Current.Data.Student_ID_Number
loop
         Previous:= Current;
         Current:= Current.Next;
       end loop;
       -- The nodes before and after the new ID number have been found.
       -- Insert between these.
       Temp.Next:= Current;
       Previous.Next:= Temp;
     end if;

Here Student_ID_Number is the key field in the record being passed in. This
is just one piece of an if elsif constuct that handles all 4 cases.
You could then write a proceedure that summarizes the list to the screen.
 Trying to store & summarize a singly linked-list in a single proceedure can
be done, but I think it would work better with 2. Better still with the
operations for the ADT in the package body & then a user interface/test
program seperate (assuming this is a CS homework assignment).

PS: I would make this ADT private & include  exported user-defined
exceptions  as well.

Supreme wrote in message <345CD154.1249E04E@eng.ua.edu>...
>In the following program I am trying to display the values of linked
>list instead my output is
>80 80 80 80 80 80
>instead of 40 50 60 70 80.
>here it is.
>procedure problem is
>    type LIST_NODE;
>    type NODE_PTR is access LIST_NODE;
>    type LIST_NODE is record
>                NUM : INTEGER;
>                NEXT : NODE_PTR;
>     end record;
>
>HEAD,P  : NODE_PTR;
>N, NUM_NODES : INTEGER;
>begin -- lab10A
>    Put("Please enter the the values of the nodes");
>Get( Num_Nodes);
>Head := new NODE;
>New_Line;
>P := Head;
>    for K in 2..NUN_NODES loop
>Head.Next := P;
>P.ALL.NUM := N;
>P := P.Next;
>end loop;
>    Will this allow teh user to assing 20 30 40 50 etc to the linked
>list?
>in other words
>Head.Next := P Should point the Head.next to the Pointer P and
>then P.ALL.NUM :=N should be set to the value of N  ( entered by the
>user)
>then P := P.Next should allow N to be assigned to the next pointer.
>is this right??
>Peace.
>




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

end of thread, other threads:[~1997-11-04  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-11-02  0:00 Code Confusion Supreme
1997-11-02  0:00 ` bklungle
1997-11-04  0:00 ` Bill

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