Limited_Lists
Linked list with some nice features.
• Private implementation: can't mess around with this.
• Small memory footprint: only one extra pointer per item.
• Built-in iterator for simple loops.
• Modify while iterating: insert, remove, update.
• Item direct iteration: unlimited parallel independent iterators.
• Direct access to first and last item: can add items at both ends.
• Cached item count.
| | type Content_Type | |
| Generic type of the items to be stored. | |
| | type List_Access | |
| Instance of a list, needs to be created before it can be used. | |
| | type Item | |
| Instance of a list item. Most useful if you need multiple iterators. | |
List construction
| | function Create | |
| Create a list. | |
|
| | procedure Append | |
| Append a list at the end of another list. This is done by simply
linking from the last item to the first item of the tail, not by
copying the items of the tail. This means that if you modify the
tail list later, the modifications will appear in the appended
list as well. | |
|
Adding items
| | function Push | |
| Insert an item at the end of the list. | |
|
| | function Unshift | |
| Insert an item at the beginning of the list. | |
|
Removing items
| | function Pop | |
| Remove an item at the end of the list. | |
|
| | function Shift | |
| Remove an item at the beginning of the list. | |
|
Item indexing
| | function Count | |
| Count items in this list. This operation is not O(n) but O(1)
because the result comes straight from a counter variable. | |
|
| | function Empty | |
| Is the list empty? Returns True if and only if there are no
items in the list. | |
|
| | function Index | |
| Read the current iteration position as a number, starting with 1
at the first item. An index of 0 means: no current item, so
either we're not currently iterating or the iteration has
finished. | |
|
First and last item
| | function First | |
| Read the first item in the list. | |
|
| | function Last | |
| Read the last item in the list. | |
|
Iterating over a list
| | procedure Reset | |
| Reset the iteration index to 0. This starts a new
iteration. There must be a call to Next before the first item
can be read from the function Current. | |
|
| | procedure Next | |
| Go to the next item. | |
|
| | function Next | |
| Go to the next item. Returns False if and only if we reached the
end of the list. | |
|
| | function Next_Available | |
| Is the a next item available? Returns False if the current item
is the last one in the list. | |
|
| | function Next_Content | |
| Get the content of the next item. | |
|
| | function End_Of_List | |
| End of list reached? Returns True if and only if the current
item pointer is null. This also occurs after calling Reset but
before calling Next. | |
|
| | function Current | |
| Read the item at the current iteration pointer. | |
|
Manipulating a list while iterating
| | procedure Remove_Current | |
| Remove the item at the current iteration pointer from the list. | |
|
| | function Insert_Before_Current | |
| Insert an item directly before the item at the current iteration
pointer. | |
|
| | function Insert_After_Current | |
| Insert an item directly after the item at the current iteration
pointer. | |
|
Item direct iteration
| | function First_Item | |
| Get the first item of the list. | |
|
| | function Last_Item | |
| Get the last item of the list. | |
|
| | function Current_Item | |
| Get the current iteration item of the list. | |
|
| | procedure Next_Item | |
| Go to the next item. | |
|
| | function Next_Item | |
| Get the next item. | |
|
| | function Item_Invalid | |
| Returns True if and only if the item pointer is null. This
happens after the last item in the list. | |
|
| | function Item_Equals | |
| Compare two items (not the contents but the pointers). | |
|
| | function Item_Content | |
| Read an item's content. | |
|
| | procedure Remove_Item | |
| Remove an item from the list and advance the item pointer. | |
|
|