Sha256: 08fcf4900cac67d64c0418c9dfbb932887f20842981937a91d04499a034ab352
Contents?: true
Size: 911 Bytes
Versions: 346
Compression:
Stored size: 911 Bytes
Contents
class Element attr_reader :datum attr_accessor :next def initialize(value) @datum = value @next = nil end end class SimpleLinkedList def initialize(array = []) create_from_array(array) end def to_a result = [] pointer = @head while pointer result << pointer.datum pointer = pointer.next end result end def push(element) element.next = @head unless @head.nil? @head = element self end def pop element = @head @head = element.next if element element end def reverse! previous = nil pointer = @head while pointer temp = pointer.next pointer.next = previous previous = pointer pointer = temp end @head = previous self end private def create_from_array(array) array.each { |value| push(Element.new(value)) } end end module BookKeeping VERSION = 1 end
Version data entries
346 entries across 346 versions & 1 rubygems