单向链表的实现

来源:互联网 发布:手机联系淘宝人工客服 编辑:程序博客网 时间:2024/05/29 18:16
/*单向链表的实现*/
class Node{
private String data;
private Node next;
public Node(String data){
this.data = data;
}
public String getData(){
return this.data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class LinkDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
Node root = new Node("火车头");
Node n1 = new Node("第一节车厢");
Node n2 = new Node("第二节车厢");
Node n3 = new Node("第三节车厢");
root.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
prinNode(root);
}

public static void prinNode(Node node){
System.out.println(node.getData()+"  ");
if(node.getNext()!=null){
prinNode(node.getNext());
}
}


}
0 0