From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e433da2465aede0e X-Google-Attributes: gid103376,public From: "Bill" Subject: Re: Code Confusion. Date: 1997/11/04 Message-ID: <63p118$4l0@bolivia.earthlink.net>#1/1 X-Deja-AN: 287147877 References: <345CD154.1249E04E@eng.ua.edu> X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3 Organization: EarthLink Network, Inc. Newsgroups: comp.lang.ada Date: 1997-11-04T00:00:00+00:00 List-Id: 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. >