Java链表

public class Node {
		//每一个链表实际上就是由多个节点组成的 
		private String data; //用于保存数据
		private Node next;   //用于保存下一个节点
			
		public Node(String data){  //每一个Node类对象都必须保存有数据
				this.data = data ;
		}
			
		public void setNext(Node next){
				this.next = next ;
		}
			
		public Node getNext(){
				return this.next ;
		}
			
		public String getData(){
				return this.data ;
		}
}

  

public class LinkdeTest {
    
    public static void main(String[] args) {
            
        //第一步:准备数据
        Node root = new Node("一闪") ;
        Node n1 = new Node("一闪") ;
        Node n2 = new Node("亮晶晶") ;
        Node n3 = new Node("满天") ;
        Node n4 = new Node("都是") ;
        Node n5 = new Node("小星星") ;
        // 链接节点
        root.setNext(n1);
        n1.setNext(n2);
        n2.setNext(n3);
        n3.setNext(n4);
        n4.setNext(n5);
        
        //第二步:取出所有数据
        Node currentNode = root ;  //从当前根节点开始读取
        while( currentNode !=  null){
            System.out.println(currentNode.getData()) ;
            //将下一个节点设置为当前节点s
            currentNode = currentNode.getNext() ;
        }
 }
}

 

posted @ 2020-03-13 13:08  雪瞳  阅读(73)  评论(0编辑  收藏  举报