Skip to content

Instantly share code, notes, and snippets.

@OfTheDelmer
Last active December 30, 2015 01:19
Show Gist options
  • Save OfTheDelmer/7755672 to your computer and use it in GitHub Desktop.
Save OfTheDelmer/7755672 to your computer and use it in GitHub Desktop.
###
# Node List
###
class Node
attr_accessor :val, :next
def initialize(val,next=nil)
@val=val
@next=next
end
end
class List
attr_accessor :items
def initialize
# When it initializes its items are nil
@items = nil
end
def insert_first(item)
# Set @items to a new Node with
# next = @items
# val = item to be added
@items = Node.new(item,@items)
end
# Just made these methods
def head
@items.val
end
def tail
@items.next
end
end
# Define a function that takes an Array and returns a list
def toList(array, myList=List.new)
myList.insert_first(array.pop)
if array == []
myList
else
toList(array, myList)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment