没有将所有元素添加到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++;
        }
    }
}

每次调用insertLast时,isEmpty返回true,因为head.nextnull。只有当isEmpty返回false时,head.next才会被设置为非空

此检查:

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

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

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

你应该初始化为:

this.head = null;

Hai程序的小改动,不需要初始化head。下一个为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;
    }
}

相关内容

  • 没有找到相关文章

最新更新