IonicWind Software

IWBasic => General Questions => Topic started by: joeb on April 19, 2009, 11:53:35 PM

Title: Linked list question
Post by: joeb on April 19, 2009, 11:53:35 PM
Here's my list creation...

TYPE clouds
   INT x,y
   INT exist
   INT dir
   INT xplod
   INT frame
ENDTYPE
cloudlist= LISTCREATE
   for t=1 to 10
      pcloud = ListAdd(cloudlist,NEW(clouds,1))
      #<clouds>pcloud.x = RAND(0,1)*760
      #<clouds>pcloud.y = 100+t*25
      #<clouds>pcloud.exist = 1
      #<clouds>pcloud.dir = -rand(1,10)
      IF #<clouds>pcloud.x=0 THEN #<clouds>pcloud.dir = rand(1,10)
      #<clouds>pcloud.xplod =0
      #<clouds>pcloud.frame=rand(1,4)
   next t


How can I assign pcloud.x & .y(or any of the others) to other list or variable? Random node 1 thru 10?

I'm confused ??? Hope this makes sense.

Thanks
Joe

Title: Re: Linked list question
Post by: Ionic Wind Support Team on April 20, 2009, 12:20:23 AM
Joe,
Lists are sequential, one record after another tied together with pointers, they don't have random access like an array does.

To get to a particular node you have to iterate through the list until you get to the one you want.

See FOR EACH in the users guide for an example of list iteration.

Paul.
Title: Re: Linked list question
Post by: Boris on April 20, 2009, 12:58:03 AM
Maybe you can make a note of the node pointers (pcloud) in an array. Then you can directly reference any record in the list. Just don’t try to reference deleted records.
Title: Re: Linked list question
Post by: joeb on April 20, 2009, 09:11:53 PM
Thanks Paul... I had a hunch(no command to access them that way..  :-[ ) that's the way they work. I was hoping. Time to rewrite some code.

Title: Re: Linked list question
Post by: Ionic Wind Support Team on April 20, 2009, 09:38:26 PM
Joe,
Your choice of data structure depends on the number of elements, whether you need to expand the data, and how fast you want lookups to be. 

I use linked lists all the time with thousands of entries, they are convenient because they can be expanded quickly with no overhead. Insertions are equal to O(n) which means that to insert an element at the 100th position requires traversing the list, one element after another, until you get to the 100th position.  Adding to the head or tail position is equal to O(1) which means that the the list has a pointer to the head and tail and can immediately add the node.  They are commonly used in games to store lists of objects.

A dynamic array (created with NEW) is faster for lookups (accessing with [ ] operator) but expanding a dynamic array requires much more time as the existing data needs to be copied, unless you are using a memory object that can be reallocated without losing data. See the oop_array.eba example the comes with Emergence BASIC.

If your data can be associated with a unique key such as a word or number then using a hashed array (dictionary or associative array) is the best choice.

Paul.

Title: Re: Linked list question
Post by: joeb on April 21, 2009, 02:15:48 PM
Thanks, I came to the same solution; for what I'm doing arrays are the way to go.

You also answered my next question about speed. Thanks

Joe