Class: DoublyLinkedList

Inherits:
LinkedList show all
Defined in:
lib/honey_mushroom/doubly_linked_list.rb

Instance Attribute Summary collapse

Attributes inherited from LinkedList

#head

Instance Method Summary collapse

Methods inherited from LinkedList

#find

Constructor Details

#initializeDoublyLinkedList

Returns a new instance of DoublyLinkedList.



6
7
8
9
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 6

def initialize
  super
  @tail = nil
end

Instance Attribute Details

#tailObject

Returns the value of attribute tail.



5
6
7
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 5

def tail
  @tail
end

Instance Method Details

#add(value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 11

def add(value)
  node = Node.new({value: value, next: nil, last: nil})
  node.next = @head
  @head = node
  node.next.last = node unless node.next.nil?
  @tail = node if @tail.nil?
end

#remove_backObject



27
28
29
30
31
32
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 27

def remove_back
  current = @tail
  @tail = current.last
  current.last.next = nil
  return current
end

#remove_frontObject



19
20
21
22
23
24
25
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 19

def remove_front
  current = @head
  @head = current.next
  current.next.last = nil

  return current
end

#to_sObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/honey_mushroom/doubly_linked_list.rb', line 35

def to_s
  s = '@head<->'
  current = @head
  until current.nil?
    s += "[#{current.value}]<->"
    current = current.next
  end

  return s
end