Monkey Documentation

Class List<T>

A linked list is a container style data structure that provides efficient support for the addition, removal and sequential traversal of objects. More...


Extended by:
Constructors:
Methods:

Detailed Discussion

A linked list is a container style data structure that provides efficient support for the addition, removal and sequential traversal of objects.

A linked list works by connecting elements together with 'next' and 'previous' references, making it very efficient to get from one element to the next, but not so efficient for accessing arbitrary elements.

This connection between elements is achieved using separate Node objects (there is one per element) which contain references to the next and previous nodes in the list, as well as the actual object managed by the node.


Constructor Documentation

Method New ()

Creates a new, initially empty list object.

Method New ( data:T[] )

Creates a new list object initialized with an array of values.


Method Documentation

Method AddFirst : Node<T> ( data:T )

Adds a value at the start of the list.

Method AddLast : Node<T> ( data:T )

Adds a value at the end of the list.

Method Backwards : Object ()

Returns an object that may be used to iterate backwards through the list with a For EachIn loop.

Note that this doesn't not actually reverse or modify the list in any way.

Example

Function Main()

    Local list:=New StringList
    
    list.AddLast "Hello"
    list.AddLast "there"
    list.AddLast "this"
    list.AddLast "is"
    list.AddLast "a"
    list.AddLast "test"
    
    Print "Fowards:"
    For Local t$=EachIn list
        Print t
    Next
    
    Print ""
    
    Print "Backwards:"
    For Local t$=Eachin list.Backwards()
        Print t
    Next

End

Method Clear : Int ()

Removes all the elements from the list.

Method Compare : Int ( lhs:T, rhs:T )

This method is used by the Sort method to compare elements.

This method must return a negative value if lhs < rhs, a positive value if lhs > rhs, or 0 if lhs = rhs.

By default, this method simply generates a runtime error. Extending classes should implement this method if they want to support Sort.

Method Contains : Bool ( value:T )

Returns true if list contains value.

See also Equals

Method Count : Int ()

Returns the number of elements in the list.

Note that this method takes time proportional to the number of elements in the list.

Method Equals : Bool ( lhs:T, rhs:T )

This method is used by the Contains, RemoveFirst, RemoveLast and RemoveEach methods to determine element equality.

By default, this method compares lhs and rhs using the '=' operator. Extending classes may override this method to provide their own equality test.

Method Find : Node<T> ( value:T )

Finds the node containing the first element in the list equal to value.

If no matching node is found, Null is returned.

Method FindLast : Node<T> ( value:T )

Finds the node containing the last element in the list equal to value.

If no matching node is found, Null is returned.

Method First : T ()

Returns the first element in the list.

Using this method on an empty list will cause a runtime error in debug mode, and undefined behavior in release mode.

Method FirstNode : Node<T> ()

Return the first node in the list, or Null if the list is empty.

Method InsertAfter : Node<T> ( where:T, data:T )

Inserts data after the first element in the list equal to where.

Method InsertAfterEach : Void ( where:T, data:T )

Inserts data after each element in the list equal to where.

Method InsertBefore : Node<T> ( where:T, data:T )

Inserts data before the first element in the list equal to where.

Method InsertBeforeEach : Void ( where:T, data:T )

Inserts data before each element in the list equal to where.

Method IsEmpty : Bool ()

Return True if the list is empty, else False.

Method Last : T ()

Returns the last element in the list.

Using this method on an empty list will cause a runtime error in debug mode, and undefined behavior in release mode.

Method LastNode : Node<T> ()

Returns the last node in the last, or Null if the list is empty.

Method ObjectEnumerator : Object ()

Returns an object enumerator for use with For Eachin loops.

Method RemoveEach : Int ( value:T )

Finds and removes each element in the list equal to value.

See also Equals

Method RemoveFirst : T ()

Removes the first value in the list and returns it.

Using this method on an empty list will cause a runtime error in debug mode, and undefined behavior in release mode.

Method RemoveFirst : Void ( value:T )

Finds and removes the first element in the list equal to value.

See also Equals

Method RemoveLast : T ()

Removes the last value in the list and returns it.

Using this method on an empty list will cause a runtime error in debug mode, and undefined behavior in release mode.

Method RemoveLast : Void ( value:T )

Finds and removes the last element in the list equal to value.

See also Equals

Method Sort : Int ( ascending:Int=true )

Sorts the list into ascending or descending order.

The list must implement the Compare method.

Method ToArray : T[] ()

Converts the list to an array of values.