Class: NodeList
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
Attributes inherited from Node
Instance Method Summary collapse
- #append(value) ⇒ Object
- #delete_node_with_val(value) ⇒ Object
- #get_node_with_val(value) ⇒ Object
-
#initialize(value) ⇒ NodeList
constructor
A new instance of NodeList.
Constructor Details
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head.
4 5 6 |
# File 'lib/node-list.rb', line 4 def head @head end |
Instance Method Details
#append(value) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/node-list.rb', line 10 def append(value) if @head.next.nil? @head.next = Node.new(value) else pointer = @head temp = pointer until pointer.next.nil? pointer = temp.next temp = pointer end pointer.next = Node.new(value) end end |
#delete_node_with_val(value) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/node-list.rb', line 36 def delete_node_with_val(value) return @head.value = @head.next if @head.value == value previous = @head pointer = @head.next temp = pointer loop do raise "No node found with value #{value}." if pointer.nil? if pointer.value == value previous.next = temp.next return @head end previous = pointer pointer = temp.next temp = pointer end end |
#get_node_with_val(value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/node-list.rb', line 24 def get_node_with_val(value) return @head if @head.value == value pointer = @head.next temp = pointer loop do return pointer if pointer.value == value raise "No Node with the value: '#{value}' was found in this node list." if pointer.next.nil? pointer = temp.next temp = pointer end end |