Java 单链表娱乐

Java Singly-Linked-List Recreation

所以对于一个赋值,我需要基本上制作一个单链表,有两种方法,add(x)这会在列表的末尾添加一个新节点,以及一个从中删除的deleteMin()列表结尾

最下面是我做的代码。我一直在第 66

行收到错误 java.lang.NullPointerException

head.next = u;

的 add(x) 方法中

我曾多次尝试更改代码以修复此错误,但似乎没有任何效果。

package assignment1;

import java.util.InputMismatchException;
import java.util.Scanner;

public class SLL {
    private int item;
    private SLL next;

    SLL(int x, SLL y){
        item = x;
        next = y;
    }

    public static SLL head;
    public static SLL tail;
    public static int n;

    public static void main(String[] args){
        boolean x = true;
        Scanner user_input = new Scanner(System.in);
        System.out.println("A = add element, R = remove element, L = to list items inside Singly-Linked-List, Q = to stop session");
        while (x == true) {
            String user = user_input.next();
            String userLow = user.toLowerCase();
            switch (userLow) {
                case "a":
                    System.out.println("Add an integer");
                    int a;
                    try {
                        a = user_input.nextInt();
                    } catch (InputMismatchException e) {
                        System.out.println("Incorect input, please input an integer.");
                        break;
                    }
                    System.out.println("");
                    add(a);
                    break;
                case "r":
                    deleteMin();
                    break;
                case "l":
                    //list();
                    break;
                case "q":
                    x = false;
                    break;
                case "help":
                    System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                    break;
                case "?":
                    System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                    break;
                default:
                    System.out.println("Not a recognized command: try 'help' or '?' for a list of commands");
                    break;
            }
        }
    }

    public static void add(int x){
        SLL u = new SLL(x, head);
        if (n == 0) {
            head.next = u; // <<<------------
            head.next.next = tail;
        } else {
            SLL current = head;
            for (int i = 0; i < n; i++) {
                current = current.next;
            }
            current.next = u;
            current.next.next = tail;
        }
        n++;
    }
    public static void deleteMin(){
        if (n == 0) {
            System.out.println("No elements inside list");
        } else if (n == 1) {
            head.next = null;
            tail.next = null;
            n--;
        } else {
            head.next = head.next.next;
            head.next.next = null;
            n--;
        }
    }
}

您尚未初始化 head,但请尝试访问它的 next 属性。这导致 NullPointerException,因为当时 headnull

您需要用某物初始化您的 head 变量。在您尝试访问它的值之前喜欢 head = new SSL(item, next)

当您在该行中设置下一个属性并且您的 SSL 构造函数也设置了该属性时,您可能希望将 head.next = u; 替换为 head = new SSL(x, u);

按照您的操作方式,下一行也会生成 NullPointerException,因为当您分配 head.next.next = tail; 时,head.nextnull。这来自 head 当您将它交给 SLL u = new SLL(x, head); 中的构造函数时 null
此外,这条线可能无论如何都无效,因为此时 tail 也是 null。所以你基本上做 head.next.next = null;