java实现单链表的整表创建

package com.java.dataStruct;


public class Node<E> {
    
    E item;
    Node next;
    public Node(){
    }
    public Node(E element){
        this.item = element;
    }
    public Node(E element, Node next){
        this.item = element;
        this.next = next;
    }
    

}
        Node p;
        
        Node L = new Node<String>("head");//创建头节点
        L.next = null;
        
        // 整表创建 - 头插法
        for(int i=1; i<=20; i++){
            p = new Node<String>();
            p.item = "value"+i;//给节点赋值
            
            p.next = L.next;
            L.next = p;
        }
        
        while(L.next != null){
            System.out.println(L.next.item);
            L = L.next;
        }
        int size = 0;
        Node p,r;
        
        Node L = new Node<String>("head");
        r = L;
        
        // 整表创建 - 尾插法
        for(int i=1; i<=20; i++){
            p = new Node<String>();
            p.item = "value"+i;
            
            r.next = p;
            r = p;
            
            size ++;
        }
        r.next = null;
        
        while(L.next != null){
            System.out.println(L.item);
            System.out.println(L.next.item);
            L = L.next;
        }

 

posted on 2016-11-28 21:35  wzyy  阅读(401)  评论(0编辑  收藏  举报