java实现单链表的整表创建

时间:2022-05-20 16:53:46
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=; i<=; 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 = ;
Node p,r; Node L = new Node<String>("head");
r = L; // 整表创建 - 尾插法
for(int i=; i<=; 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;
}