Node
open class Node<T>
A type to hold onto elements in a LinkedList.
Discussion
These nodes hold onto a value, the next node, and the previous node.
-
The concrete value the node is holding on to.
Declaration
Swift
public var value: Value -
An optional reference to the next node in the
LinkedList.Declaration
Swift
public var next: Node<Value>? -
An optional reference to the previous node in the
LinkedList.Declaration
Swift
public var previous: Node<Value>? -
A method to move N spaces forwards or backwards through the nodes.
Important
If the distance is out of bounds nil will be returnedDeclaration
Swift
open func traverse(_ distance: Int) -> Node<T>?Parameters
distancean integer indicating how far to move through the nodes.
Return Value
the node at the indicated distance; nil if distance is out of bounds.
-
A method to move forward through the nodes until a precondition is met.
Declaration
Swift
open func traverse(direction: TraversalDirection = .forward, until: ((Node<T>) -> Bool)) -> Node<T>?Parameters
directionan enum indicating whether to traverse forward or backwards, defaults to
TraversalDirection.forward.untila closure that takes in a node and returns a boolean to indicate whether traversal should continue. Once until returns true, it is not called again.
Return Value
the node when traversal finishes; nil if none found.
-
A method to move forward through the nodes until there is no
next.Declaration
Swift
open func traverseToEnd() -> Node<T> -
A method to move backwards through the nodes until there is no
previous.Declaration
Swift
open func traverseToBeginning() -> Node<T> -
A nodes position in the
LinkedListComplexity
O(n) this has a worst case of having to traverse the entire list to determine its position.Declaration
Swift
open var position: Int { get } -
Creates an exact replica of the node, including the next and previous values, this essentially deep copies the entire
LinkedList.Declaration
Swift
open func copy() -> Node<T>
View on GitHub
Node Class Reference