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

Version Path
trackler-2.2.1.119 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.118 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.117 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.116 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.115 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.114 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.113 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.111 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.110 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.109 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.108 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.107 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.106 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.105 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.104 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.103 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.102 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.101 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.100 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb
trackler-2.2.1.99 tracks/ruby/exercises/simple-linked-list/.meta/solutions/simple_linked_list.rb