ddeveloperr
2/6/2016 - 12:20 AM

## Algorithm and Data Structures With Ruby - #7 LinkedList

Algorithm and Data Structures With Ruby - #7 LinkedList

``````# Quick Example of a Self Referential Data Structure in Ruby
# NODE -> contains a value and a pointer to (next_node)

class Node

attr_accessor :value, :next_node

def initialize val,next_in_line
@value = val
@next_nodex = next_in_line
puts "Initialized a Node with value:  " + value.to_s
end
end

def initialize val
# Initialize a new node at the head
end

# Traverse to the end of the list
# And insert a new node over there with the specified value
while current.next_node != nil
current = current.next_node
end
current.next_node = Node.new(value,nil)
self
end

def delete(val)
if current.value == val
# If the head is the element to be delete, the head needs to be updated
else
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
#   ... x->z
# ( and z is basically y.next_node )
while (current != nil) && (current.next_node != nil) && ((current.next_node).value != val)
current = current.next_node
end

if (current != nil) && (current.next_node != nil)
current.next_node = (current.next_node).next_node
end
end
end

def display
# Traverse through the list till you hit the "nil" at the end
full_list = []
while current.next_node != nil
full_list += [current.value.to_s]
current = current.next_node
end
full_list += [current.value.to_s]
puts full_list.join("->")
end

end

# Initializing a Linked List with a node containing value (5)

ll.display

puts "Delete 10 and then display the linked list:"
ll.delete(10)
ll.display

=begin
Output:
Initialized a Node with value:  5
Initialized a Node with value:  10
Initialized a Node with value:  20