Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created February 18, 2010 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jutememo/307278 to your computer and use it in GitHub Desktop.
Save jutememo/307278 to your computer and use it in GitHub Desktop.
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