Class linkedlist.double
A double-linked-list class.
The class features the following functionality:
- API compatible with the single-linked-list (easy to switch)
- adds lifo queue behaviour.
- adds method to swap 2 items in position
- adds methods for moving items up/down
- adds a reverse iterator
The item
objects are mostly for internal use only. The actual value stored lives
in the item.value
field.
Methods additional to the Single linked list interface
List:get_previous ([item]) | Gets the previous item. |
List:move_down (item) | Moves an item 1 element down. |
List:move_up (item) | Moves an item 1 element up. |
List:peek_lifo () | Returns the value at the end (lifo), without removing it. |
List:pop_lifo () | Pops the value at the end (lifo). |
List:reverse_iter () | Iterator over the list, in reverse order. |
List:swap (item1, item2) | Swaps two items. |
Single linked list interface
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. |
Methods additional to the Single linked list interface
- List:get_previous ([item])
-
Gets the previous item.
Parameters:
- item item an item object as returned when added, if not provided, the last item in the list will be returned. (optional)
Returns:
-
the previous/last item, or nil+"not found"
- List:move_down (item)
-
Moves an item 1 element down.
Moves 1 position towards the end of the list. Does nothing
if the item is already at the end of the list.
Parameters:
- item item must be an item object as returned when added.
Returns:
-
true if successful, false if already at the end
- List:move_up (item)
-
Moves an item 1 element up.
Moves 1 position towards the start of the list. Does nothing
if the item is already at the top of the list.
Parameters:
- item item must be an item object as returned when added.
Returns:
-
true if successful, false if already at the top
- List:peek_lifo ()
-
Returns the value at the end (lifo), without removing it.
Returns:
-
the value at the end, or
nil+"not found"
if empty - List:pop_lifo ()
-
Pops the value at the end (lifo).
Returns:
-
the value at the end, or
nil+"not found"
if empty - List:reverse_iter ()
-
Iterator over the list, in reverse order.
Note: the returned
count
will be from 1 tosize
, ascending.Returns:
-
iterator returning count and value
Usage:
for cnt, value in my_list:iter() do print(value) end
- List:swap (item1, item2)
-
Swaps two items.
Updates the positions of the two items in the list. If the items are the same
item, nothing happens.
Parameters:
- item1 item must be an item object as returned when added.
- item2 item must be an item object as returned when added.
Returns:
-
nothing
Single linked list interface
This part is the same interface that the Single linked list has. The interface is
identical, so it should be easy to update from Single to Double.
- 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