Skip to content

ListNode[A: A]

[Source]

A node in a doubly linked list.

(See Ponylang 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 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
...


```pony
class ref ListNode[A: A]

Constructors

create

[Source]

Create a node. Initially, it is not in any list.

new ref create(
  item: (A | None val) = reference)
: ListNode[A] ref^

Parameters

  • item: (A | None val) = reference

Returns


Public Functions

apply

[Source]

Return the item, if we have one, otherwise raise an error.

fun box apply()
: this->A ?

Returns

  • this->A ?

update

[Source]

Replace the item and return the previous one. Raise an error if we have no previous value.

fun ref update(
  value: (A | None val))
: A^ ?

Parameters

  • value: (A | None val)

Returns

  • A^ ?

pop

[Source]

Remove the item from the node, if we have one, otherwise raise an error.

fun ref pop()
: A^ ?

Returns

  • A^ ?

prepend

[Source]

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.

fun ref prepend(
  that: ListNode[A] ref)
: Bool val

Parameters

Returns


append

[Source]

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.

fun ref append(
  that: ListNode[A] ref)
: Bool val

Parameters

Returns


remove

[Source]

Remove a node from a list.

The ListNode must be contained within a List for this to succeed.

fun ref remove()
: None val

Returns


has_prev

[Source]

Return true if there is a previous node.

fun box has_prev()
: Bool val

Returns


has_next

[Source]

Return true if there is a next node.

fun box has_next()
: Bool val

Returns


prev

[Source]

Return the previous node.

fun box prev()
: (this->ListNode[A] ref | None val)

Returns


next

[Source]

Return the next node.

fun box next()
: (this->ListNode[A] ref | None val)

Returns