October 29, 2025, 11:38:23 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Linked list question

Started by joeb, April 19, 2009, 11:53:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

joeb

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


Ionic Wind Support Team

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.
Ionic Wind Support Team

Boris

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.
Thank you for not breeding

joeb

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.


Ionic Wind Support Team

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.

Ionic Wind Support Team

joeb

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