Linked Lists

A linked list allows you to efficiently add and remove objects to a collection of objects.

To create a linked list, use the CreateList command.

You add objects to a linked list using ListAddFirst or ListAddLast. Both commands return a link object which can be used to later remove the added object with the RemoveLink command. You can also remove objects with the ListRemove command. 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 a For...EachIn loop.

Function reference

CreateList:TList()   Create a linked list

Returns: A new linked list object

Example:

' createlist.bmx

' create a list to hold some objects

list:TList=CreateList()

' add some string objects to the list

ListAddLast list,"one"
ListAddLast list,"two"
ListAddLast list,"three"

' enumerate all the strings in the list

For a$=EachIn list
	Print a$
Next

ClearList( list:TList )   Clear a linked list

Removes all objects from list.


CountList( list:TList )   Count list length

Returns: The numbers of objects in list.


ListIsEmpty( list:TList )   Check if list is empty

Returns: True if list is empty, else false


ListContains( list:TList,value:Object )   Check if list contains a value

Returns: True if list contains value, else false


SortList( list:TList,ascending=True )   Sort a list

ListFromArray:TList( arr:Object[] )   Create a list from an array

Returns: A new linked list


ListToArray:Object[]( list:TList )   convert a list to an array

Returns: An array of objects


SwapLists( list_x:TList,list_y:TList )   Swap the contents of 2 lists

ReverseList( list:TList )   Reverse the order of elements of a list


ListAddLast:TLink( list:TList,value:Object )   Add an object to a linked list

Returns: A link object


ListAddFirst:TLink( list:TList,value:Object )   Add an object to a linked list

Returns: A link object


ListRemove( list:TList,value:Object )   Remove an object from a linked list

ListRemove scans a list for the specified value and removes its link.



Module Information

Modulebrl.linkedlist
Version 1.00
Author Mark Sibly
License Blitz Shared Source Code
Copyright Blitz Research Ltd
Modserver BRL