Wikibooks: BlitzMax/Modules/Data structures/Linked lists

A linked list allows you to efficiently add and remove objects to and from a collection of objects. To create a linked list use the [[CreateList]] function. Add objects to a linked list using [[ListAddFirst]] or [[ListAddLast]]. Both functions return a link object which can be used to later remove t...

Full description

Bibliographic Details
Format: Book
Language:English
Subjects:
Online Access:https://en.wikibooks.org/wiki/BlitzMax/Modules/Data_structures/Linked_lists
Description
Summary:A linked list allows you to efficiently add and remove objects to and from a collection of objects. To create a linked list use the [[CreateList]] function. Add objects to a linked list using [[ListAddFirst]] or [[ListAddLast]]. Both functions return a link object which can be used to later remove the object with the [[RemoveLink]] function. You can also remove objects with the [[ListRemove]] function. However this is not as efficient as using [[RemoveLink]] because the list must first be searched for the object to be removed. To visit all the objects in a linked list you can use an [[EachIn]] loop. =Lists versus Arrays= It is important to keep in mind that index based access in a linked list is relatively slow. For each item that you wish to index the linked list must iterate through its internal links until it finds the requested index whereas an array can directly access any value at any position. So why use linked lists at all? you might ask. Inserting a value into the middle of an array is an expensive operation you must move all items after the insert position down in the array by one then insert the new value in the space created. In a linked list you must simply redirect the pointers between links and you have instantly inserted a value in the middle of the list. It is generally best to use a linked list when you need to make many changes to data and an array when you need to frequently access data at random. If you need to make many insertions and deletions to an array you can convert it to a list with [[ListFromArray]] and then convert the edited list back to an array with [[ListToArray]]. =Types= =TLink= Link Object used by TList hiddenh4 Methods Value NextLink PrevLink Remove =TLink Methods= hiddenh3 Value Method Value Object() Description Returns the Object associated with this Link. Example value.bmx Local list TList = New TList list.AddLast( Item 1 ) list.AddLast( Item 2 ) list.AddLast( Item 3 ) Local link TLink = list.FindLink( Item 2 ) Print String(link.Value()) hiddenh3 NextLink Method NextLink ...