Class LinkedList
A doubly-linked List of items.
A List implementation that stores items as nodes that point backward and forward to each other for traversal. This provides good insertion, and deletion characteristics as well as access to the beginning and end of the list, but does so at the expense of random access and search complexity.
Insertion, deletion, and access to the beginning and end are Θ(1). Access elsewhere, and search are Θ(n).
LinkedList implements all optional List and Collection methods.
Extends: AbstractList
Implements: List, Collection, Enumerable
Functions
| LinkedList.new(collection) | Creates a new LinkedList. |
Methods
| LinkedList:Add(item) | Adds an item to the LinkedList. |
| LinkedList:Clear() | Removes everything from the LinkedList. |
| LinkedList:Count() | Gets the number of items in the LinkedList. |
| LinkedList:Delete(index) | Removes the item at the specified index from the LinkedList. |
| LinkedList:Enumerator() | Creates an enumerator for the LinkedList. |
| LinkedList:First() | Gets the item at the beginning of the LinkedList. |
| LinkedList:Get(index) | Gets the item at the specified index in the LinkedList. |
| LinkedList:IndexOf(item, index) | Determines the index of a specific item in the LinkedList. |
| LinkedList:Insert(index, item) | Inserts the item into the LinkedList at the specified index. |
| LinkedList:InsertAll(index, items) | Inserts multiple items into the LinkedList at the specified index. |
| LinkedList:Last() | Gets the item at the end of the LinkedList. |
| LinkedList:LastIndexOf(item) | Determines the last index of a specific item in the LinkedList. |
| LinkedList:Pop() | Gets an item from the end and removes that item from the LinkedList. |
| LinkedList:Push(item) | Adds an item to the end of the LinkedList. |
| LinkedList:Remove(item) | Removes the specified item from the LinkedList. |
| LinkedList:RetainAll(items) | Removes all items except those provided from the LinkedList. |
| LinkedList:Set(index, item) | Sets the element at the specified index. |
| LinkedList:Shift() | Gets an item from the beginning and removes that item from the LinkedList. |
| LinkedList:Sub(first, last) | Creates a new sub-list of this LinkedList. |
| LinkedList:Unshift(item) | Adds an item to the beginning of the LinkedList. |
| LinkedList:addNode(value, node) | Adds a new node to the LinkedList. |
| LinkedList:removeNode(node) | Remove a node from the LinkedList. |
Functions
Methods- LinkedList.new(collection)
-
Creates a new LinkedList.
Creates a LinkedList copy of the provided Collection or array-indexed
table if one is provided, otherwise creates an empty LinkedList.
Parameters:
- collection the Collection or table to copy
Returns:
-
the new LinkedList
Methods
- LinkedList:Add(item)
-
Adds an item to the LinkedList.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the LinkedList is always changed
- LinkedList:Clear()
-
Removes everything from the LinkedList.
Inherited from:
- LinkedList:Count()
-
Gets the number of items in the LinkedList.
Inherited from:
Returns:
-
the number of items
- LinkedList:Delete(index)
-
Removes the item at the specified index from the LinkedList.
Shifts other elements to fill the gap left at the index of removal.
Inherited from:
Parameters:
- index the index of the item to remove from the LinkedList
Returns:
-
true always since the LinkedList is always changed
Raises:
if the index is out of bounds of the LinkedList - LinkedList:Enumerator()
-
Creates an enumerator for the LinkedList.
The enumerator can be used directly in a generic for loop similar to pairs
or ipairs.
Inherited from:
Returns:
-
the enumerator generator
- LinkedList:First()
-
Gets the item at the beginning of the LinkedList.
Inherited from:
Returns:
-
the first item in the LinkedList
Raises:
if the LinkedList is empty - LinkedList:Get(index)
-
Gets the item at the specified index in the LinkedList.
Inherited from:
Parameters:
- index the index to get
Returns:
-
the item in the LinkedList at the specified index
Raises:
if the index is out of bounds of the LinkedList - LinkedList:IndexOf(item, index)
-
Determines the index of a specific item in the LinkedList.
Starts from a specified index or from the beginning if none is provided.
Inherited from:
Parameters:
- item the item to locate
- index the index to start looking from
Returns:
-
the index of the item in the LinkedList if found, 0 otherwise
Raises:
if the index is out of bounds of the LinkedList - LinkedList:Insert(index, item)
-
Inserts the item into the LinkedList at the specified index.
Shifts other elements to make space at the index of insertion.
Inherited from:
Parameters:
- index the index to insert the item in the LinkedList
- item the item to add
Returns:
-
true always since the LinkedList is always changed
Raises:
if the index is out of bounds of the LinkedList - LinkedList:InsertAll(index, items)
-
Inserts multiple items into the LinkedList at the specified index.
Inserts all items from the provided Collection in the order they are
enumerated. Shifts other elements to make space at the index of insertion.
Inherited from:
Parameters:
- index the index to insert the items in the LinkedList
- items the Collection of items to add to this LinkedList
Returns:
-
true always since the LinkedList is always changed
Raises:
if the index is out of bounds of the LinkedList - LinkedList:Last()
-
Gets the item at the end of the LinkedList.
Inherited from:
Returns:
-
the last item in the LinkedList
Raises:
if the LinkedList is empty - LinkedList:LastIndexOf(item)
-
Determines the last index of a specific item in the LinkedList.
Only returns the very last occurrence of the item in the LinkedList.
Inherited from:
Parameters:
- item the item to locate
Returns:
-
the index of the item in the LinkedList if found, 0 otherwise
- LinkedList:Pop()
-
Gets an item from the end and removes that item from the LinkedList.
Inherited from:
Returns:
-
the item in the LinkedList
Raises:
if the LinkedList is empty - LinkedList:Push(item)
-
Adds an item to the end of the LinkedList.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the LinkedList is always changed
- LinkedList:Remove(item)
-
Removes the specified item from the LinkedList.
Removes only a single item. If there are multiple of the same item, it
removes only the first encountered.
When an item is removed any others are shifted to fill the gap left at the index of removal.
Inherited from:
Parameters:
- item the item to remove from the LinkedList
Returns:
-
true if the LinkedList changed as a result, false otherwise
- LinkedList:RetainAll(items)
-
Removes all items except those provided from the LinkedList.
Retains only the items contained in the specified Collection. If there are
duplicates they are all kept.
Inherited from:
Parameters:
- items the Collection of items to retain in this LinkedList
Returns:
-
true if the LinkedList changed as a result, false otherwise
- LinkedList:Set(index, item)
-
Sets the element at the specified index.
Inherited from:
Parameters:
- index the index to set
- item the item to set at the index
Returns:
-
true if the LinkedList changed as a result, false otherwise
Raises:
if the index is out of bounds of the LinkedList - LinkedList:Shift()
-
Gets an item from the beginning and removes that item from the LinkedList.
Shifts other elements to fill the gap left.
Inherited from:
Returns:
-
the item in the LinkedList
Raises:
if the LinkedList is empty - LinkedList:Sub(first, last)
-
Creates a new sub-list of this LinkedList.
Creates the list that is the portion of this LinkedList between the
specified indices or from the first specified index to the end if only one
index is specified.
Inherited from:
Parameters:
- first the index to start at
- last the index to end at
Returns:
-
the new LinkedList
Raises:
- if the first index is out of bounds
- if the last index is out of bounds
- if the last index is smaller than the first index
- LinkedList:Unshift(item)
-
Adds an item to the beginning of the LinkedList.
Shifts other elements to make space.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the LinkedList is always changed
- LinkedList:addNode(value, node)
-
Adds a new node to the LinkedList.
Inserts the node infront of the node provided or at the end of the
LinkedList if none is provided.
Access:
-
private
Parameters:
- value the value to add a new node for
- node the node to insert before
- LinkedList:removeNode(node)
-
Remove a node from the LinkedList.
Access:
-
private
Parameters:
- node the node to remove