ListNode[A: A]¶
A node in a doubly linked list.
See Pony collections.List class for usage examples.
Each node contains four fields: two link fields (references to the previous and to the next node in the sequence of nodes), one data field, and the reference to the List in which it resides.
As you would expect functions are provided to create a ListNode, update a ListNode's contained item, and pop the item from the ListNode.
Additional functions are provided to operate on a ListNode as part of a Linked List. These provide for prepending, appending, removal, and safe traversal in both directions. The Ponylang collections.List class is the correct way to create these. Do not attempt to create a Linked List using only ListNodes.
Example program¶
The functions which are illustrated below are only those which operate on an individual ListNode.
It outputs:
My node has the item value: My Node item My node has the updated item value: My updated Node item Popped the item from the ListNode The ListNode has no (None) item.
use "collections"
actor Main
new create(env:Env) =>
// Create a new ListNode of type String
let my_list_node = ListNode[String]("My Node item")
try
env.out.print("My node has the item value: "
+ my_list_node.apply()?) // My Node item
end
// Update the item contained in the ListNode
try
my_list_node.update("My updated Node item")?
env.out.print("My node has the updated item value: "
+ my_list_node.apply()?) // My updated Node item
end
// Pop the item from the ListNode
try
my_list_node.pop()?
env.out.print("Popped the item from the ListNode")
my_list_node.apply()? // This will error as the item is now None
else
env.out.print("The ListNode has no (None) item.")
end
Constructors¶
create¶
Create a node. Initially, it is not in any list.
Parameters¶
- item: (A | None val) = reference
Returns¶
- ListNode[A] ref^
Public Functions¶
apply¶
Return the item, if we have one, otherwise raise an error.
Returns¶
- this->A ?
update¶
Replace the item and return the previous one. Raise an error if we have no previous value.
Parameters¶
- value: (A | None val)
Returns¶
- A^ ?
pop¶
Remove the item from the node, if we have one, otherwise raise an error.
Returns¶
- A^ ?
prepend¶
Prepend a node to this one. If that
is already in a list, it is removed
before it is prepended. Returns true if that
was removed from another
list.
If the ListNode is not contained within a List the prepend will fail.
Parameters¶
- that: ListNode[A] ref
Returns¶
- Bool val
append¶
Append a node to this one. If that
is already in a list, it is removed
before it is appended. Returns true if that
was removed from another
list.
If the ListNode is not contained within a List the append will fail.
Parameters¶
- that: ListNode[A] ref
Returns¶
- Bool val
remove¶
Remove a node from a list.
The ListNode must be contained within a List for this to succeed.
Returns¶
- None val
has_prev¶
Return true if there is a previous node.
Returns¶
- Bool val
has_next¶
Return true if there is a next node.
Returns¶
- Bool val
prev¶
Return the previous node.
Returns¶
next¶
Return the next node.