Java单链接列表重新创建

时间:2015-02-11 23:53:16

标签: java singly-linked-list

因此,对于一项作业,我需要基本上使用两种方法制作单链表,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--;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

您需要使用sth初始化head变量。与head = new SSL(item, next)类似,然后再尝试访问它的值。

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

您执行此操作的方式,下一行也会产生NullPointerException,因为当您指定head.next.next = tail;时,head.nextnull。当您将head传递给null中的构造函数时,这来自SLL u = new SLL(x, head);。 此外,此行可能无效,因为此时tail也是null。所以你基本上做head.next.next = null;