【问题标题】:Java LinkedList addLast() method implementation error [duplicate]Java LinkedList addLast() 方法实现错误[重复]
【发布时间】:2021-05-02 04:45:06
【问题描述】:

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

这里是错误信息:

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

进程以退出代码 1 结束


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

这是我的代码

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

}

【问题讨论】:

    标签: java linked-list singly-linked-list


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2021-09-18
      • 2011-02-26
      • 2012-11-15
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多