class CircularLinked::List
Attributes
head[RW]
Public Class Methods
new(*values)
click to toggle source
# File lib/circular_linked/list.rb, line 6 def initialize(*values) nodes = values.map {|v| Node.new(v) } @head = nodes.first nodes.each_with_index {|node, i| node.next = nodes[i+1] || @head } end
Public Instance Methods
add(value)
click to toggle source
# File lib/circular_linked/list.rb, line 12 def add(value) new_node = Node.new(value, head) @head ||= new_node new_node.next ||= head last_node.next = new_node end
each() { |node| ... }
click to toggle source
# File lib/circular_linked/list.rb, line 73 def each return unless head node = head loop do yield(node) node = node.next break if node == head end end
find(value)
click to toggle source
# File lib/circular_linked/list.rb, line 29 def find(value) return nil unless head node = head while node.value != value node = node.next return nil if node == head end node end
items_value()
click to toggle source
# File lib/circular_linked/list.rb, line 57 def items_value values = [] return values unless head each {|node| values.push(node.value) } values end
last_node()
click to toggle source
# File lib/circular_linked/list.rb, line 21 def last_node return nil unless head node = head node = node.next while node.next != head node end
length()
click to toggle source
# File lib/circular_linked/list.rb, line 65 def length length = 0 return length unless head each { length += 1 } length end
remove(value)
click to toggle source
# File lib/circular_linked/list.rb, line 41 def remove(value) node = find(value) return unless node if head.next == head @head = nil return end node_before = head node_before = node_before.next while node_before.next != node node_before.next = node.next @head = node.next if node == head end