Created
October 29, 2011 17:53
-
-
Save philipschwarz/1324854 to your computer and use it in GitHub Desktop.
List
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
public class List | |
{ | |
public static final List EMPTY_LIST = new List(); | |
public List(int n, List rest) | |
{ | |
this.n = n; | |
this.rest = rest; | |
} | |
public boolean isEmpty() { return this == EMPTY_LIST; } | |
public int head() { return n; } | |
public List tail() { return rest; } | |
public List cons(int n) { return new List(n, this); } | |
public List reverse() | |
{ | |
List list = this; | |
List reversed = EMPTY_LIST; | |
while(!list.isEmpty()) | |
{ | |
reversed = new List(list.head(), reversed); | |
list = list.tail(); | |
} | |
return reversed; | |
} | |
private List rest; | |
private int n; | |
private List() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment