Class linkedlist.single
A single-linked-list class.
The class features the following functionality:
- it is nil-safe. Any values, including nils can be stored.
- has push/pop/peek semantics for (fifo) queue behaviour.
- keeps track of first + last item, for cheap pushing/popping.
- has methods for indexing (array-like), getting/storing values (though not efficient)
The item
objects are mostly for internal use only. The actual value stored lives
in the item.value
field.
Traversing the list:
local item = lst:get_next() while item do print(item.value) item = lst:get_next(item) end
Functions
List.new () | Create a new Linked List object. |
List:add_value (value[, idx=size+1]) | Adds an item to the list. |
List:clear () | Clears the list. |
List:first_item () | Gets the first item in the list. |
List:first_value () | Gets the first value in the list. |
List:get_item (idx) | Gets the item at the specified index. |
List:get_next ([item]) | Gets the next item. |
List:get_size () | Gets the length of the list. |
List:get_value (idx) | Gets the value at the specified index. |
List:iter () | Iterator over the list. |
List:last_item () | Gets the last item in the list. |
List:last_value () | Gets the last value in the list. |
List:peek () | Returns the value at the top/start, without removing it. |
List:pop () | Pops the value at the top/start. |
List:push (value) | Adds a value to the end of the list. |
Functions
Methods- List.new ()
-
Create a new Linked List object.
Returns:
-
Linked List object
Usage:
local lst = List.new() -- or alternatively local lst = List()
- List:add_value (value[, idx=size+1])
-
Adds an item to the list.
Note that the
idx
position, is the position where the item ends up after insertion. Too high or too low indices will not return errors, but insert at the start/end.Parameters:
- value
the value to store in the list, can be any type, inlcuding
nil
- idx
int
position where to add (negative indices allowed,
idx = 0
will add at the start). By default appends at end. (default size+1)
Returns:
-
the added item.
- value
the value to store in the list, can be any type, inlcuding
- List:clear ()
-
Clears the list.
Returns:
-
true
- List:first_item ()
-
Gets the first item in the list.
Does not remove the item from the list.
Returns:
-
the first item, or nil+"not found"
- List:first_value ()
-
Gets the first value in the list.
Does not remove the value from the list.
Returns:
-
the first value, or nil+"not found"
- List:get_item (idx)
-
Gets the item at the specified index.
Note: this method iterates over the list to find the index, so it is
not efficient, especially with larger lists.
Parameters:
- idx int index of the item to return
Returns:
-
the item, or nil+"not found"
- List:get_next ([item])
-
Gets the next item.
Parameters:
- item item an item object as returned when added, if not provided, the first item in the list will be returned. (optional)
Returns:
-
the next/first item, or nil+"not found"
- List:get_size ()
-
Gets the length of the list.
Returns:
-
integer, length of the list
- List:get_value (idx)
-
Gets the value at the specified index.
Note: this method iterates over the list to find the index, so it is
not efficient, especially with larger lists.
Parameters:
- idx int index of the value to return
Returns:
-
the value, or nil+"not found"
- List:iter ()
-
Iterator over the list.
Returns:
-
iterator returning idx and value
Usage:
for i, value in my_list:iter() do print(value) end
- List:last_item ()
-
Gets the last item in the list.
Does not remove the item from the list.
Returns:
-
the last item
- List:last_value ()
-
Gets the last value in the list.
Does not remove the value from the list.
Returns:
-
the last value
- List:peek ()
-
Returns the value at the top/start, without removing it.
Returns:
-
the value at the start, or
nil+"not found"
if empty - List:pop ()
-
Pops the value at the top/start.
Returns:
-
the value at the start, or
nil+"not found"
if empty - List:push (value)
-
Adds a value to the end of the list.
Parameters:
- value
the value to store in the list, can be any type, inlcuding
nil
Returns:
-
the added item.
- value
the value to store in the list, can be any type, inlcuding