繁体   English   中英

没有将所有元素添加到链表中

[英]Not adding all of the elements into the linkedlist

为什么我的insertLast(T data)方法不将所有元素都添加到列表中?

public class Tester {
    public static void main(String[] args){
        LinkedList<Integer> myList = new LinkedList<Integer>();
        myList.insertLast(1);
        myList.insertLast(2);
        myList.insertLast(3);
        myList.insertLast(4);
        myList.insertLast(5);
        myList.insertLast(6);
        myList.displayList();
    }
}

它仅添加6。代码可能是什么问题?

public class Node<T> {

    public T data;
    public Node<T> next;

    public Node(T data, Node<T> n){
        this.data = data;
        this.next = n;
    }

    public void display(){
        System.out.print(this.data + " ");
    }
}

class LinkedList<T> implements Iterable<T>{

    private Node<T> head;
    private int size;

    public LinkedList(){
        this.head = new Node<T>(null, null);
        this.size = 0;
    }

    public boolean isEmpty(){
        return (head.next == null);
    }

    public void displayList(){
        Node<T> current = head;
        while(current != null){
            current.display();
            current = current.next;
        }
    }

    public void insert(T data){
        head = new Node<T>(data, null);
        size++;
    }

    public void insertLast(T data){
        Node<T> newNode = new Node<T>(data, null);
        if(isEmpty()){
            head = new Node<T>(data, null);
            size++;
        }
        else{
            Node<T> current = head;
            while(current.next != null){
                current = current.next;
            }
            current.next = newNode;
            size++;
        }
    }
}

每当head.nextnull ,每次调用insertLastisEmpty返回true。 仅当isEmpty返回false时, head.next才设置为非null。

此检查:

if(isEmpty()){
      head = new Node<T>(data, null);
      size++;
  }

初始化head使其下一个为null ,因此isEmpty()每次调用都返回true。 您需要在isEmpty()和构造函数中检查head本身是否为null,而不是:

this.head = new Node<T>(null, null);

您应该将其初始化为:

this.head = null;

在程序中进行Hai Minor更改。您不必将head.next初始化为null

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

public class Node<T> {

    public T data;
    public Node<T> next;

    public Node(T data, Node<T> n){
        this.data = data;
        this.next = n;
    }

    public void display(){
        System.out.print(this.data + " ");
    }
}

class LinkedList<T> implements Iterable<T>{

    private Node<T> head;
    private int size;

    public LinkedList(){
       // this.head = new Node<T>(null, null);
        this.size = 0;
    }

    public boolean isEmpty(){
        return (head == null);
    }

    public void displayList(){
        Node<T> current = head;
        while(current != null){
            current.display();
            current = current.next;
        }
    }

    public void insert(T data){
        head = new Node<T>(data, null);
        size++;
    }

    public void insertLast(T data){
        Node<T> newNode = new Node<T>(data, null);
        if(isEmpty()){
            head = new Node<T>(data, null);
            size++;
        }
        else{
            Node<T> current = head;
            while(current.next != null){
                current = current.next;
            }
            current.next = newNode;
            size++;
        }
    }

    public void forEach(Consumer<? super T> arg0) {
        // TODO Auto-generated method stub

    }

    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    public Spliterator<T> spliterator() {
        // TODO Auto-generated method stub
        return null;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM