Created
September 15, 2011 06:05
-
-
Save bryanbibat/1218653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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