Class: DoublyLinkedList
- Inherits:
-
LinkedList
- Object
- LinkedList
- DoublyLinkedList
- Defined in:
- lib/honey_mushroom/doubly_linked_list.rb
Instance Attribute Summary collapse
-
#tail ⇒ Object
Returns the value of attribute tail.
Attributes inherited from LinkedList
Instance Method Summary collapse
- #add(value) ⇒ Object
-
#initialize ⇒ DoublyLinkedList
constructor
A new instance of DoublyLinkedList.
- #remove_back ⇒ Object
- #remove_front ⇒ Object
- #to_s ⇒ Object
Methods inherited from LinkedList
Constructor Details
#initialize ⇒ DoublyLinkedList
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
#tail ⇒ Object
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_back ⇒ Object
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_front ⇒ Object
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_s ⇒ Object
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 |