Sha256: e8550cc907ce1b3ad4c54aa2697a0edd9bc7c9d383cb6704cad01c8f34b3e75a

Contents?: true

Size: 840 Bytes

Versions: 396

Compression:

Stored size: 840 Bytes

Contents

class Element
  attr_accessor :prev
  attr_accessor :next
  attr_reader :datum

  def initialize(datum, next_element = nil, prev = nil)
    @datum = datum
    @next = next_element || self
    @prev = prev || self
  end
end

class Deque
  def initialize
    @front = nil
  end

  def push(value)
    if @front.nil?
      @front = Element.new(value)
    else
      back = @front.prev
      n = Element.new(value, @front, back)
      back.next = n
      @front.prev = n
    end
  end

  def unshift(value)
    push(value)
    @front = @front.prev
  end

  def pop
    @front = @front.prev
    shift
  end

  def shift
    value = @front.datum
    front = @front.next
    back = @front.prev
    if front.equal?(@front)
      @front = nil
    else
      front.prev = back
      back.next = front
      @front = front
    end
    value
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.179 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.178 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.177 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.176 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.175 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.174 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.173 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.172 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.171 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.170 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.169 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.167 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.166 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.165 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.164 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.163 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.162 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.161 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb
trackler-2.2.1.160 tracks/ruby/exercises/linked-list/.meta/solutions/linked_list.rb