Skip to content

Instantly share code, notes, and snippets.

@asterite
Created February 19, 2014 14:03
Show Gist options
  • Save asterite/9092692 to your computer and use it in GitHub Desktop.
Save asterite/9092692 to your computer and use it in GitHub Desktop.
class LinkedList(T)
include Enumerable
class Node(T)
property :next
property :data
def initialize(@data : T, @next = nil)
end
end
def add(value : T)
node = Node.new(value)
if @last
@last.next = node
@last = node
else
@first = @last = node
end
end
def each
node = @first
while node
yield node.data
node = node.next
end
end
def to_s
String.build do |str|
str << "["
each_with_index do |value, i|
str << ", " if i > 0
str << value
end
str << "]"
end
end
end
list = LinkedList(Int32).new
list.add(1)
list.add(2)
list.add(3)
puts list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment