Java LinkedList addLast() 方法实现错误

Java LinkedList addLast() method implementation error

所以我在 Java 中实现了一个单链表。但是,我的 addLast() 方法一直抛出 NullPointerException。

错误信息如下:

线程“main”中出现异常 java.lang.NullPointerException:无法分配字段“next”,因为“list.tail”为空 在 SinglyLinkedList.SLinkedList.addLast(SLinkedList.java:39) 在 SinglyLinkedList.SLinkedList.main(SLinkedList.java:98)

进程已完成,退出代码为 1


我不明白为什么 tail 为 null。任何帮助将不胜感激。

这是我的代码

public class SLinkedList {
    private SNode head;
    private SNode tail;
    private static int size;

    private static class SNode {
        String name;
        SNode next;

        //constructor
        SNode(String name) {
            this.name = name;
            next = null;
        }
    }

    public static SLinkedList insertNode(SLinkedList list, String name) {
        SNode newNode = new SNode(name);
        newNode.next = null;

        if (list.head == null) {
            list.head = newNode;
        } else {
            SNode last = list.head;
            while (last.next != null) {
                last = last.next;
            }
            last,next = newNode;
        }
        size++;
        return list;
    }

    public static SLinkedList addLast(SLinkedList list, String name){
        SNode newNode = new SNode(name);

        list.tail.next = newNode;
        list.tail = list.tail.next;

        size++;
        //return the list
        return list;
    }

    // a method that add head to the list
    public static SLinkedList addFirst(SLinkedList list, String input) {
        SNode newNode = new SNode(input);
        newNode.next = list.head;
        if (list.head == null) {
            list.tail = newNode;
        }
        list.head = newNode;
        size++;
        return list;
    }

    //a method to remove the head of the list
    public static String removeFirst(SLinkedList list) throws Exception {
        SNode temp = list.head;
        // edge cases
        // size 0
        if (size == 0) {
            throw new Exception("The LinkedList is empty.");
        }

        list.head = temp.next;
        temp.next = null;
        size--;

        // edge cases
        // size 0
        if (size == 0) {
            list.tail = null;
        }

        return temp.name;
    }

    public static void printSList(SLinkedList list) {
        SNode current = list.head;
        System.out.print("Singly LinkedList is: ");

        while (current != null) {
            System.out.print(current.name + " ");
            current = current.next;
        }
    }

    //driver code
    public static void main(String[] args) throws Exception {
        SLinkedList list = new SLinkedList();

        insertNode(list, "nuggie");
        insertNode(list, "cloud");

        addLast(list, "cotton");
        // addLast(list, "jamie");
        // addLast(list, "pot-roast");

        addFirst(list, "kennedy");
        addFirst(list, "hoegaarden");

        System.out.println("The element removed is " + removeFirst(list));

        System.out.println("Size of the Singly Linked List is " + size);
        printSList(list);
    }

}

您的链接列表的 tail 元素尚未初始化。在 insertNode 你需要更新你的 tail:

if (list.head == null) {
    list.head = newNode;    
} else {
    SNode last = list.head;
    while (last.next != null) {
        last = last.next;
    }
    last.next = newNode;
}
list.tail = newNode; // add this