class NodeList

Attributes

head[RW]

Public Class Methods

new(value) click to toggle source
# File lib/node-list.rb, line 6
def initialize(value)
  @head = Node.new(value)
end

Public Instance Methods

append(value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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