Class: LinkedList::Doubly
- Inherits:
-
Object
- Object
- LinkedList::Doubly
- Defined in:
- lib/data_struct/linked_list.rb
Overview
This is a Doubly linked list implementation.
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #each ⇒ Object
- #index(index_val) ⇒ Object
- #index_of(val) ⇒ Object
-
#initialize ⇒ Doubly
constructor
A new instance of Doubly.
- #pop ⇒ Object
- #push(val) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unshift(val) ⇒ Object
Constructor Details
#initialize ⇒ Doubly
Returns a new instance of Doubly.
126 127 128 129 |
# File 'lib/data_struct/linked_list.rb', line 126 def initialize @head = nil @length = 0 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
125 126 127 |
# File 'lib/data_struct/linked_list.rb', line 125 def head @head end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
125 126 127 |
# File 'lib/data_struct/linked_list.rb', line 125 def length @length end |
Instance Method Details
#each ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/data_struct/linked_list.rb', line 187 def each node = @head arr = [] @length.times do arr << yield(node.data) node = node.next_val end arr end |
#index(index_val) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/data_struct/linked_list.rb', line 197 def index(index_val) raise 'index value higher than current length of list' if @length - 1 < index_val node = @head index_val.times do node = node.next_val end node.data end |
#index_of(val) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/data_struct/linked_list.rb', line 207 def index_of(val) node = @head @length.times do return node.data if node.data == val node = node.next_val end raise 'value not found' end |
#pop ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/data_struct/linked_list.rb', line 152 def pop node = @head if @length.zero? raise 'Error: List is empty' else de_length (@length - 1).times do node = node.next_val end node.next_val = nil end @head end |
#push(val) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/data_struct/linked_list.rb', line 140 def push(val) in_length return @head = DoublyNode.new(val) if @head.nil? node = @head (@length - 2).times do node = node.next_val end node.next_val = DoublyNode.new val node.next_val.pre_val = node end |
#to_a ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/data_struct/linked_list.rb', line 177 def to_a node = @head arr = [] @length.times do arr << node.data node = node.next_val end arr end |
#to_s ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/data_struct/linked_list.rb', line 167 def to_s node = @head str = '' (@length - 1).times do str += "#{node.data} " node = node.next_val end str end |
#unshift(val) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/data_struct/linked_list.rb', line 131 def unshift(val) in_length return @head = DoublyNode.new(val) if @head.nil? new_head = DoublyNode.new val, nil, @head @head.pre_val = new_head @head = new_head end |