Skip to content

Instantly share code, notes, and snippets.

@jonathanmarvens
Created October 26, 2013 03:40
Show Gist options
  • Save jonathanmarvens/7165039 to your computer and use it in GitHub Desktop.
Save jonathanmarvens/7165039 to your computer and use it in GitHub Desktop.
module DataStructures
class NaiveLinkedList
def initialize()
@head = nil
end
def [](index)
node = @head
self.loop_set_node(index, node)
node.value()
end
def []=(index, value)
node = @head
self.loop_set_node(index, node)
node.value = value
end
def <<(value)
new_node = Node.new(value)
if @head != nil
node = @head
while node.other() != nil
node = node.other()
end
node.other = new_node
else
@head = new_node
end
end
def loop_set_node(iterations, node)
(0...iterations).each() do
node = node.other()
end
end
end
end
module DataStructures
class Node
attr_accessor(:other)
attr_accessor(:value)
def initialize(value, other = nil)
@other = other
@value = value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment