Skip to content

Class: LinkedList

Michael Ebens edited this page Jun 22, 2018 · 2 revisions

The LinkedList implements the doubly linked list data structure. It is used internally by World to store entities, but you may find it quite useful yourself.

Each element must be a table; to link them together LinkedList sets properties in them specifying the next and previous elements. By default these properties are named _next and _prev, but you can specify different names in the constructor, allowing for the same table to be added to multiple lists.

Properties

all
Returns all the list's elements in a table. This involves looping through all the elements, so access this only when necessary.

first
The first element of the list.

last
The last element of the list.

length
The number of elements in the list.

Methods

initialize(nextProp, prevProp, ...) Initialises the list.

nextProp: The property set in each element to specify the next element. Defaults to "_next".
prevProp: The property set in each element to specify the previous element. Defaults to "_prev".

clear(complete)
Clears the list of all elements. By default it just resets first, last, and length. However, if you specify complete as true, it will loop through all elements and set their references to the next and previous elements to nil.

complete: Whether or not to do a complete clear as described above. Defaults to false.

insert(node, after)
Inserts node in the list after after. If you do not specify after, and no elements are in the list, node will be added as the first element.

node: The element to insert.
after: The element to insert node after. Defaults to nil.

iterate(reverse)
Returns an iterate you can use to iterate through all the elements in the list. Example:

for e in list:iterate() do
  print(e.x + e.y)
end

reverse: If true, the iterator returned will traverse the list in reverse. Defaults to false.

pop()
Removes and returns the last element.

push(node)
Adds node to the end of the list.

node: The element to add.

remove(node)
Removes node from the list.

node: The element to remove.

shift()
Removes and returns the first element.

unshift(node)
Adds node to the beginning of the list.

node: The element to add.

Clone this wiki locally