Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!husc6!sri-unix!hplabs!hpcea!hpfcdc!hpfcrj!steve From: steve@hpfcrj.UUCP Newsgroups: comp.sys.hp Subject: Re: HP Basic linked lists? Message-ID: <4680002@hpfcrj.HP.COM> Date: Tue, 24-Feb-87 23:38:32 EST Article-I.D.: hpfcrj.4680002 Posted: Tue Feb 24 23:38:32 1987 Date-Received: Fri, 27-Feb-87 22:30:06 EST References: <1084@phred.UUCP> Organization: HP Ft. Collins Lines: 69 ) /:comp.sys.hp / jeffp@phred.UUCP (Jeff Parke) / 1:32 pm Feb 18, 1987 / ) The main goal is to be able to sort a bunch of such things in BASIC and be ) able to store only as many as have been used (also in BASIC). ) ---------- The usual way to do linked lists in BASIC is to have a two-dimensional array, treat one dimension as addresses of records, with the fields across the other dimension. Since your data is all numeric, this should be no problem. Thus: Array(Ptr,Next) would be the link to the next record, Array(Ptr,Prev) " " " " " " previous record, and Array(Ptr,Datum) " " a typical data field. If the Array was dimensioned, say, Array(1:1000,0:5) then you could use 0 as a null pointer value (convenient since the Array would be initialized all zeros). Here you could have Next=0, Prev=1, and Datum=2 thru 5. However, since your data is all of one type (numeric), and in view of the goal stated above, let me suggest an alternate approach. This will work if the main data values by which you plan to sort have a bound such that an outlying value can be used to indicate unused records. If not, just add a field to the array which acts as a flag for unused records. Thus: Array(Index,Valid) is the flag field, and Array(Index,Datum) is a typical data field. Assume we use 1 for 'in use' and 0 for 'not used'. This allows you to sort the data by using the MAT SORT Array(*,Valid) DES,(*,Key_data) statement. The first key forces all the unused records to the end, and the second one sorts the valid data items according to the order of the Key_data field. The next step is to determine the amount of data in use. I generally do this with a simple loop, such as WHILE Array(Index,Valid) Index=Index+1 END WHILE If it's possible to completely fill the array, you'll also want to check for the maximum value of Index or use an ON ERROR to trap the subscript error at the top. A binary search could also be used. Now that we know how much of the array is in use, and since the sort statement has compacted the items to the beginning, REDIM Array(1:Index-1,) will tell the system to ignore the unused entries. For this to work, Index must be the first subscript and must not change. If you were able to use the sort key as a valid indicator as well, you can just do OUTPUT @File;Array(*) at this point. If you have to use a separate field for a valid flag and don't want to store it, there's always FOR-NEXT loops (don't need the REDIM in this case, should be as fast as chasing down the linked lists). Or, use two arrays, one for the valid flag and one for the data items. When you're ready to sort, use MAT SORT Valid_flags DES TO Temp then MAT REORDER Data_array BY Temp This assumes Valid_flags and Temp are one-dimensional and Data_array is subscripted as (Index,Data_field). Now find the top by WHILE Valid_flags(Temp(Index)) Index=Index+1 END WHILE and do the REDIM Data_array(1:Index-1,) Since Data_array now has only valid data, just MAT SORT Data_array(*,Key_data). And now you're ready for the OUTPUT @File;Data_array(*) Hope this is some help. Regards, y-s-t---w t-i Steve Taylor / / \ \ / \ [hpfcla!steve-t] S-O-f e-r-a o-n \ / \ \ p m-s r-e <>