Java链表基础(1)
class Node{
private String data;
private Node next;
public Node(){}
public Node(String data){
this.data = data;
}
public void setData(String data){
this.data = data;
}
public String getData(){
return this.data;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;
}
}
public class Demo1 {
public static void main(String[] args) {
Node node = new Node("Head");
Node n1 = new Node("n1");
Node n2 = new Node("n2");
Node n3 = new Node("n3");
node.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
print(node);
}
public static void print(Node next){
if(next == null)
return ;
System.out.print(next.getData()+"->");
print(next.getNext());
}
}