Skip to content

Instantly share code, notes, and snippets.

@bryanbibat
Created September 15, 2011 06:05
Show Gist options
  • Save bryanbibat/1218653 to your computer and use it in GitHub Desktop.
Save bryanbibat/1218653 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :next
def initialize(value)
@value = value
end
end
class LinkedList
attr_accessor :head, :tail
def initialize
@head = @tail = nil
end
def add_value(value)
node = Node.new(value)
if @head.nil?
@head = @tail = node
else
@tail.next = node
@tail = node
end
end
def display
return puts "empty list" if @head.nil?
current = @head
while current != @tail
puts current.value
current = current.next
end
puts current.value
end
end
ll = LinkedList.new
ll.display
puts "***"
ll.add_value(2)
ll.display
puts "***"
ll.add_value("blah")
ll.display
C:\Users\bry>ruby ll.rb
empty list
***
2
***
2
blah
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment