首页 > 代码库 > java实现单链表的整表创建

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

 

java实现单链表的整表创建