Linked List¶
| Spice | |
|---|---|
LinkedList<T> struct¶
Implements: IIterable<T>
A linked list is a common, dynamically resizable data structure to store uniform data in order. It is characterized by the pointer for each item, pointing to the next one.
E.g. for a LinkedList
1234 -> 4567 -> 7890 -> 4567 -> nil
tail head
Time complexity:
Insert: O(1)
Delete: O(1)
Search: O(n)
Beware that each add operation allocates memory and every remove operation frees memory.
Methods¶
pushBack¶
| Spice | |
|---|---|
Pushes a new item to the back of the list
Parameters
| Name | Type | Description |
|---|---|---|
value |
const T& |
Value to push |
pushFront¶
| Spice | |
|---|---|
Pushes a new item to the front of the list
Parameters
| Name | Type | Description |
|---|---|---|
value |
const T& |
Value to push |
insertAt¶
| Spice | |
|---|---|
Inserts a new item at the given index
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index to insert the new item |
value |
const T& |
Value to insert |
insertAt¶
| Spice | |
|---|---|
Inserts a new item at the given index
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned int |
Index to insert the new item |
value |
const T& |
Value to insert |
remove¶
| Spice | |
|---|---|
Removes the first occurrence of the given value
Parameters
| Name | Type | Description |
|---|---|---|
valueToRemove |
const T& |
Value to remove |
removeAt¶
| Spice | |
|---|---|
Removes the first occurrence of the given value
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
removeAt¶
| Spice | |
|---|---|
Removes the first occurrence of the given value
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned int |
Index to remove |
removeFront¶
| Spice | |
|---|---|
Removes the first item of the list
removeBack¶
| Spice | |
|---|---|
Removes the last item of the list
getSize¶
| Spice | |
|---|---|
Returns the size of the list
Returns: unsigned long — Size of the list
isEmpty¶
| Spice | |
|---|---|
Returns if the list is empty
Returns: bool — true if the list is empty, false otherwise
get¶
| Spice | |
|---|---|
Returns the item at the given index
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index to access |
Returns: T& — Reference to the item
get¶
| Spice | |
|---|---|
Returns the item at the given index
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned int |
Index to access |
Returns: T& — Reference to the item
getFront¶
| Spice | |
|---|---|
Returns the first item of the list
Returns: T& — Reference to the first item
getBack¶
| Spice | |
|---|---|
Returns the last item of the list
Returns: T& — Reference to the last item
clear¶
| Spice | |
|---|---|
Clears the list
getIterator¶
| Spice | |
|---|---|
Retrieve a forward iterator for the linked list
Returns: LinkedListIterator<T>
LinkedListIterator<T> struct¶
Implements: IIterator<T>
Iterator to iterate over a linked list data structure
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a linked list iterator over the given linked list
Parameters
| Name | Type | Description |
|---|---|---|
list |
LinkedList<T>& |
Linked list to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current item of the linked list
Returns: T& — Reference to the current item
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current item of the linked list
Returns: Pair<unsigned long, T&> — Pair of current index and reference to current item
isValid¶
| Spice | |
|---|---|
Check if the iterator is valid
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Returns the current item of the linked list iterator and moves the cursor to the next item
Operators¶
operator++¶
| Spice | |
|---|---|
Advances the cursor by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
LinkedListIterator<T>& |
LinkedListIterator |
operator--¶
| Spice | |
|---|---|
Move the cursor back by one
Parameters
| Name | Type | Description |
|---|---|---|
it |
LinkedListIterator<T>& |
LinkedListIterator |
operator+=¶
| Spice | |
|---|---|
Advances the cursor by the given offset
Parameters
| Name | Type | Description |
|---|---|---|
it |
LinkedListIterator<T>& |
LinkedListIterator |
offset |
Numeric |
Offset |
operator-=¶
| Spice | |
|---|---|
Move the cursor back by the given offset
Parameters
| Name | Type | Description |
|---|---|---|
it |
LinkedListIterator<T>& |
LinkedListIterator |
offset |
Numeric |
Offset |