Skip to content

Commit

Permalink
Add node iterators for list
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Mar 20, 2023
1 parent 7927e1c commit 4410d23
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,21 @@ func (n *Node[V]) EachReverse(fn func(val V)) {
node = node.Prev
}
}

// EachNode calls 'fn' on every node from this node onward in the list.
func (n *Node[V]) EachNode(fn func(n *Node[V])) {
node := n
for node != nil {
fn(node)
node = node.Next
}
}

// EachReverseNode calls 'fn' on every node from this node backward in the list.
func (n *Node[V]) EachReverseNode(fn func(n *Node[V])) {
node := n
for node != nil {
fn(node)
node = node.Prev
}
}

0 comments on commit 4410d23

Please sign in to comment.