class Algorithmix::DataStructure::Generic::LinkedList::Iterator

Iterator class which gives couple of useful methods, for moving through the elements of a given list.

Attributes

current[R]
next_node[R]

Public Class Methods

new(node) click to toggle source
# File lib/algorithmix/data_structure/generic/linked_list.rb, line 388
def initialize(node)
  raise ArgumentError, "Invalid argument" unless node.is_a?(Node) || node.is_a?(LinkedList)

  @begin = @node.is_a?(Node) ? node : node.front
  @current = @node.is_a?(Node) ? node : node.front
  @next_node = @node.is_a?(Node) ? node : node.front

  @next_node = @next_node.next_node
end

Public Instance Methods

begin() click to toggle source

Resets the current state of the iterator. All iterators will point to the front of the list.

# File lib/algorithmix/data_structure/generic/linked_list.rb, line 400
def begin
  @current = @next_node = @begin
end
next() click to toggle source

Moves the iterator to the next position, and sets the current iterator to the previous position of the next node.

# File lib/algorithmix/data_structure/generic/linked_list.rb, line 406
def next
  raise OutOfBound, "No more elements." if @next_node.nil?
  @current = @next_node
  @next_node = @next_node.next_node
  self
end
value() click to toggle source

Returns the value at the current node.

# File lib/algorithmix/data_structure/generic/linked_list.rb, line 414
def value
  @current.value
end