【问题标题】:Display function in singlylinkedlist that accepts head of the linkedlist as a parameter在单链表中显示函数,接受链表的头部作为参数
【发布时间】:2019-04-09 18:40:47
【问题描述】:

我希望创建两个链表并编写一个显示函数,该函数接受第一个或第二个链表的头作为参数,即(一个接受第一个列表的 head1 或第二个列表的 head2 的函数)。但是,我'得到一个空指针异常。

package com.main.addtwoele;

public class LinkedList {

    Node head1, head2;

    public void insert(Node head, int data) {
        Node newNode = new Node(data);
        Node temp = head;
        head = newNode;
        newNode.next = temp;
    }
    public void display(Node head) {
        Node temp = head;
        System.out.println("---------------Linked List---------------");
        if (temp.next == null) {
            System.out.println("---Head node----");
            System.out.println(temp.data);
        }
        while (temp.next != null) {
            System.out.print(temp.data + "->");
            temp = temp.next;
        }
        System.out.println(temp.data);

    }

    public static void main(String[] args) {

        LinkedList list = new LinkedList();
        list.insert(list.head1, 50);
        list.insert(list.head1, 40);
        list.insert(list.head1, 30);
        list.insert(list.head2, 20);
        list.display(list.head1);
    }

}

Node class is as follows :-

package com.main.addtwoele;

public class Node {

    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }

}

Exception encountered :
Exception in thread "main" java.lang.NullPointerException
    at com.main.addtwoele.LinkedList.display(LinkedList.java:19)
    at com.main.addtwoele.LinkedList.main(LinkedList.java:40)

【问题讨论】:

  • edit 您的问题并标记一种语言。 java?
  • insert 函数正在修改参数而不是类成员 (head = newNode;)。见:Is Java "pass-by-reference" or "pass-by-value"?
  • 你为什么有2个头?这可能是一个设计缺陷。为什么不只拥有 2 个单独的 LinkedList 对象?
  • 如何编写插入函数来修改类成员?我不想在插入函数中传递head1或head2作为参数。
  • 我有两个头,因为我想将两个链表表示的两个数字相加。

标签: java singly-linked-list


【解决方案1】:

主要问题在于插入功能。 Java 会按值传递参数,这意味着如果您更改了函数中变量指向的内容,您将不会更改它指向函数外部的内容。

Node head1, head2;

public void insert(Node head, int data) {
    Node newNode = new Node(data);
    Node temp = head;
    head = newNode;  // This is changing what the parameter points to!
                     // Not what the class member points to
    newNode.next = temp;
}

您说您希望链接列表中有 2 个正面。我认为您不需要那个(2 个单独的链表可以工作),但要做到这一点,您可以使用 Node 对象的数组。这也将允许您修改函数中的头部对象:

Node [] heads = new Node[2];

// The head parameter is the index in the heads array 
public void insert(int head, int data) {
    Node newNode = new Node(data);
    newNode.next = heads[head];
    heads[head] = newNode;  // Works since we are not modifying what the heads member var points to.
                            // Just modifying it's contents
}

就个人而言,我只会使用一个头部对象。那你就不用把它作为参数传递了:

Node head;

public void insert(int data) {
    Node newNode = new Node(data);
    newNode.next = head;
    head = newNode;
}

如果您想要 2 个列表,则创建 2 个 LinkedList 对象。

如果headnulldisplay 函数中也会出现一个错误。所以你应该检查一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多