Class: DoublyLinkedlist::List
- Inherits:
-
Object
- Object
- DoublyLinkedlist::List
- Includes:
- Enumerable
- Defined in:
- lib/doubly_linkedlist/list.rb
Overview
Creates list with nodes
Instance Attribute Summary collapse
-
#count ⇒ Integer
readonly
The number of nodes in the list.
Instance Method Summary collapse
-
#delete_at(index) ⇒ Object
Deletes the node at a given index and returns the value present in the deleted node.
-
#each ⇒ Enumerator, DoublyLinkedlist
Support for Enumerable methods.
-
#enqueue(value) ⇒ Integer
Inserts a node with the given value into the tail of the list, increments and returns the count of nodes.
-
#find_at(index) ⇒ Object, Nil
Returns the node object at the given index if present, nil otherwise.
-
#head ⇒ Object, Nil
(also: #first)
Returns the value at the head of the list if present, nil otherwise.
-
#index(value) ⇒ Integer, Nil
Returns the leftmost index of the value present in the list.
-
#initialize(values = []) ⇒ List
constructor
Initializes the object with nodes having the values passed in the optional argument array.
-
#insert(value) ⇒ Integer
(also: #push)
Inserts a node with the given value into the head of the list, increments and returns the count of nodes.
-
#pop ⇒ Object
(also: #deque)
Pops out and returns the node_value at the list head.
-
#rindex(value) ⇒ Integer, Nil
Returns the rightmost index of value present in the list.
-
#tail ⇒ Object, Nil
(also: #last)
Returns the value at the tail of the list if present, nil otherwise.
-
#to_s ⇒ String
(also: #inspect)
Converts the list representation into a string.
Constructor Details
#initialize(values = []) ⇒ List
Initializes the object with nodes having the values passed in the optional argument array.
17 18 19 20 |
# File 'lib/doubly_linkedlist/list.rb', line 17 def initialize(values = []) @count = 0 build_list_from_array(values) end |
Instance Attribute Details
#count ⇒ Integer (readonly)
Returns the number of nodes in the list.
11 12 13 |
# File 'lib/doubly_linkedlist/list.rb', line 11 def count @count end |
Instance Method Details
#delete_at(index) ⇒ Object
Deletes the node at a given index and returns the value present in the deleted node. Returns nil if the given index is out of range.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/doubly_linkedlist/list.rb', line 106 def delete_at(index) return if (index + 1) > count || index < 0 if index.zero? deleted = @head @head = @head.next else prev_item = @head (index - 1).times { prev_item = prev_item.next } deleted = prev_item.next prev_item.next = deleted.next @tail = prev_item if prev_item.next.nil? end @count -= 1 deleted.value end |
#each ⇒ Enumerator, DoublyLinkedlist
Support for Enumerable methods.
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/doubly_linkedlist/list.rb', line 156 def each return enum_for(:each) unless block_given? item = @head while item do yield item.value item = item.next end end |
#enqueue(value) ⇒ Integer
Inserts a node with the given value into the tail of the list, increments and returns the count of nodes.
91 92 93 94 95 96 97 98 |
# File 'lib/doubly_linkedlist/list.rb', line 91 def enqueue(value) new_node = Node.new(value) @head = new_node unless @head @tail.next = new_node if @tail new_node.prev = @tail @tail = new_node @count += 1 end |
#find_at(index) ⇒ Object, Nil
Returns the node object at the given index if present, nil otherwise.
44 45 46 47 48 49 50 |
# File 'lib/doubly_linkedlist/list.rb', line 44 def find_at(index) return if (index + 1) > count || index < 0 item = @head index.times { item = item.next } item.value end |
#head ⇒ Object, Nil Also known as: first
Returns the value at the head of the list if present, nil otherwise.
25 26 27 |
# File 'lib/doubly_linkedlist/list.rb', line 25 def head @head.value if @head end |
#index(value) ⇒ Integer, Nil
Returns the leftmost index of the value present in the list. Returns nil if the value is not present.
57 58 59 |
# File 'lib/doubly_linkedlist/list.rb', line 57 def index(value) find_index(value, 0, @head, :next, 1) end |
#insert(value) ⇒ Integer Also known as: push
Inserts a node with the given value into the head of the list, increments and returns the count of nodes.
75 76 77 78 79 80 81 82 |
# File 'lib/doubly_linkedlist/list.rb', line 75 def insert(value) new_node = Node.new(value) @head.prev = new_node if @head new_node.next = @head @tail = new_node unless @tail @head = new_node @count += 1 end |
#pop ⇒ Object Also known as: deque
Pops out and returns the node_value at the list head.
129 130 131 |
# File 'lib/doubly_linkedlist/list.rb', line 129 def pop delete_at(0) end |
#rindex(value) ⇒ Integer, Nil
Returns the rightmost index of value present in the list. Returns nil if the value is not present.
66 67 68 |
# File 'lib/doubly_linkedlist/list.rb', line 66 def rindex(value) find_index(value, count - 1, @tail, :prev, -1) end |
#tail ⇒ Object, Nil Also known as: last
Returns the value at the tail of the list if present, nil otherwise.
34 35 36 |
# File 'lib/doubly_linkedlist/list.rb', line 34 def tail @tail.value if @tail end |
#to_s ⇒ String Also known as: inspect
Converts the list representation into a string.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/doubly_linkedlist/list.rb', line 138 def to_s str = "<#{self.class}: [" each_with_index do |v, i| str += v.to_s str += ', ' unless i == (count - 1) end str + ']>' end |