Created
December 2, 2013 04:08
-
-
Save OfTheDelmer/7744913 to your computer and use it in GitHub Desktop.
Playing with an attempt at Singly Linked Lists
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
| class List | |
| attr_accessor :items | |
| def initialize(*args) | |
| @items = digest(args) | |
| end | |
| def insertAfter(item, newItem, items=@items) | |
| if(items.car == item) | |
| items.cdr = Cons.new(newItem, items.cdr) | |
| else | |
| insertAfter(item, newItem,items.cdr) | |
| end | |
| end | |
| private | |
| def digest(items) | |
| if(items != []) | |
| return Cons.new(items.shift(), digest(items)) | |
| elsif(items == []) | |
| return nil | |
| end | |
| end | |
| end | |
| class Cons | |
| attr_accessor :car, :cdr | |
| def initialize(car,cdr) | |
| @car=car | |
| @cdr=cdr | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment