class DLLModule::DLL

Class Doubly Linked List

Attributes

size[R]

@attr_reader size [int] size of the doubly linked list

Public Class Methods

new(value = nil) click to toggle source

Constructor of DLL @param value of Node @param head [Node] Head of the DLL @param tail [Node] Tail of the DLL @param size [int] size of the doubly linked list

# File lib/food/dll.rb, line 23
def initialize (value = nil)
  node = Node.new(value)
  
  @head = node
  @tail = node
  @size = (node.nil?) ? 0 : 1
end

Public Instance Methods

delete(value) click to toggle source

Delete an specific element in DLL head

# File lib/food/dll.rb, line 88
def delete (value)
  current_node = @head
  
  while !current_node.nil?
    if (current_node.value == value)
      if (@size == 1) 
        @head = nil
        @tail = nil
        current_node = nil
      else
        if current_node != @head
          (current_node.prev).next = current_node.next
        end
        if current_node != @tail
          (current_node.next).prev = current_node.prev
        end
        current_node = nil
      end
      @size -= 1
    else
      current_node = current_node.next
    end
  end
end
each() { |value| ... } click to toggle source

Essential method for Enumerable

# File lib/food/dll.rb, line 131
def each
  current_node = @head
  
  while !current_node.nil? 
    yield current_node.value
    current_node = current_node.next
  end
end
empty?() click to toggle source

Return if DLL is empty

# File lib/food/dll.rb, line 126
def empty?
  return size == 0
end
extract_head() click to toggle source

Extract element in DLL head @return [Node] value of the node that was in the head

# File lib/food/dll.rb, line 53
def extract_head
  if @head.nil?
    return nil
  else
    @size -= 1
    
    node_to_return = @head
    
    @head = @head.next
    @head.prev = nil
    
    node_to_return.next = nil
    return node_to_return.value
  end
end
extract_tail() click to toggle source

Extract element in DLL tail @return [Node] value of the node that was in the tail

# File lib/food/dll.rb, line 71
def extract_tail
  if @tail.nil?
    return nil
  else
    @size -= 1
    
    node_to_return = @tail
    
    @tail = @tail.prev
    @tail.next = nil
    
    node_to_return.prev = nil
    return node_to_return.value
  end
end
get_head() click to toggle source

Return head value @return [head(value)]

# File lib/food/dll.rb, line 115
def get_head
  return @head.value
end
get_tail() click to toggle source

Return tail value @return [tail(value)]

# File lib/food/dll.rb, line 121
def get_tail
  return @tail.value
end
insert_head(*value_array) click to toggle source

Insert element in DLL head @param *value_array set of values

# File lib/food/dll.rb, line 35
def insert_head (*value_array)
  value_array.each { |value|
    node = Node.new(value)
    insert_head_private(node);
  }
end
insert_tail(*value_array) click to toggle source

Insert element in DLL tail @param *value_array set of values

# File lib/food/dll.rb, line 44
def insert_tail (*value_array)
  value_array.each { |value|
    node = Node.new(value)
    insert_tail_private(node);
  }
end

Private Instance Methods

insert_head_private(node) click to toggle source
# File lib/food/dll.rb, line 142
def insert_head_private (node)
  if @head.nil?
    @head = node
    @tail = node
  else
    node.next = @head
    @head.prev = node
    @head = node
  end
  @size += 1
end
insert_tail_private(node) click to toggle source
# File lib/food/dll.rb, line 154
def insert_tail_private (node)
  if @tail.nil?
    @head = node
    @tail = node
  else
    @tail.next = node
    node.prev = @tail
    @tail = node
  end
  @size += 1
end