有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

链表Java LinkedList addLast()方法实现错误

所以我用Java实现了一个单独的LinkedList。但是,我的addLast()方法不断抛出NullPointerException

以下是错误消息:

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

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


我不明白为什么尾巴是空的。任何帮助都将不胜感激

这是我的密码

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

}

共 (1) 个答案

  1. # 1 楼答案

    链接列表的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