Last active
December 30, 2015 01:19
-
-
Save OfTheDelmer/7755672 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ### | |
| # 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