class Containers::List

Constants

Node

Public Class Methods

[](*arr) click to toggle source
# File lib/containers/list.rb, line 9
def self.[](*arr)
  from_a(arr)
end
from_a(arr) click to toggle source
# File lib/containers/list.rb, line 13
def self.from_a(arr)
  l = new
  arr.each(&l.method(:push_front))
  l
end
new() click to toggle source
# File lib/containers/list.rb, line 4
def initialize
  @first = nil
  @last = nil
end

Public Instance Methods

empty?() click to toggle source
# File lib/containers/list.rb, line 19
def empty?
  @first == @last && @first.nil?
end
peek_back() click to toggle source
# File lib/containers/list.rb, line 73
def peek_back
  @last&.data
end
peek_front() click to toggle source
# File lib/containers/list.rb, line 69
def peek_front
  @first&.data
end
pop()
Alias for: pop_back
pop_back() click to toggle source
# File lib/containers/list.rb, line 56
def pop_back
  to_return = @last&.data
  unless @last.nil?
    @last = @last.prev
    if @last.nil?
      @first = nil
    else
      @last.next = nil unless @last.nil?
    end
  end
  to_return
end
Also aliased as: pop
pop_front() click to toggle source
# File lib/containers/list.rb, line 33
def pop_front
  to_return = @first&.data
  unless @first.nil?
    @first = @first.next
    if @first.nil?
      @last = nil
    else
      @first.prev = nil unless @first.nil?
    end
  end
  to_return
end
Also aliased as: shift
push(element)
Alias for: push_back
push_back(element) click to toggle source
# File lib/containers/list.rb, line 46
def push_back(element)
  node = Node.new(element, @last, nil)
  if @last.nil?
    @first = node
  else
    @last.next = node
  end
  @last = node
end
Also aliased as: push
push_front(element) click to toggle source
# File lib/containers/list.rb, line 23
def push_front(element)
  node = Node.new(element, nil, @first)
  if @first.nil?
    @last = node
  else
    @first.prev = node
  end
  @first = node
end
Also aliased as: unshift
shift()
Alias for: pop_front
unshift(element)
Alias for: push_front