Created
February 18, 2010 02:48
-
-
Save jutememo/307278 to your computer and use it in GitHub Desktop.
This file contains 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 Node<T> { | |
private T a; | |
private Node next; | |
public Node(T a) { | |
this.a = a; | |
} | |
// 先頭に追加する | |
Node add(T n) { | |
return new Node(n).setNext(this); | |
} | |
private Node setNext(Node n) { | |
this.next = n; | |
return this; | |
} | |
int length() { | |
if (next == null) { | |
return 1; | |
} else { | |
return 1 + this.next.length(); | |
} | |
} | |
@Override | |
public String toString() { | |
return this.next != null | |
? this.a.toString() + "," + this.next.toString() | |
: this.a.toString() + ",Nil"; | |
} | |
public static void main(String[] args) { | |
Node<Integer> n = new Node<Integer>(3).add(2).add(1); | |
System.out.println(n); | |
System.out.println(n.length()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment