码迷,mamicode.com
首页 > 编程语言 > 详细

java实现单链表的整表创建

时间:2016-11-28 22:40:06      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:尾插法   public   div   节点   ext   java   while   size   ++   

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;
        }

 

java实现单链表的整表创建

标签:尾插法   public   div   节点   ext   java   while   size   ++   

原文地址:http://www.cnblogs.com/wwzyy/p/6111486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!