微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的链接列表没有打印出来?

如何解决为什么我的链接列表没有打印出来?

我正在尝试创建一个接受用户输入的链接列表并打印相同的内容,但是,我的链接列表正在创建,但我无法print代码如下:

import java.util.Scanner;
class Node
{
    int data;
    Node next;
}
public class LinkedList
{
    public static void create(Node start)
    {
        Scanner sc = new Scanner(system.in);
        Node a = new Node();
        System.out.println("Enter Details:");
        a.data = sc.nextInt();
        a.next = null;
        start = a;
        System.out.println("Do you want to continue(Y/N)?");
        char ch = sc.next().charat(0);
        while(ch!='n')
        {
            Node b = new Node();
            System.out.println("Enter Details:");
            b.data=sc.nextInt();
            b.next = null;
            a.next = b;
            a=b;
            System.out.println("Do you want to continue(Y/N)?");
            ch = sc.next().charat(0);
        }
        sc.close();
    }
    public static void display(Node start)
    {
        Node temp = start;
        while(temp!=null)
        {
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
    public static void main(String[] args){
        Node start=null;
        create(start);
        display(start);
    }
}

我找不到问题,请帮助我找到如何print我的链接列表。

解决方法

诊断真的很简单:

    Node start=null;
    create(start);
    display(start);

您正在 display 上调用 null 方法。

问题在于您的 create 方法不会……也不能……更改 start 变量的值。要使这项工作正常进行,您必须更改 create 以便它构建并返回一个列表,然后将 create() 调用的结果分配给 {{1 }}。

请注意,您可以(并且应该)删除 start 的参数,因为它的值实际上从未在方法中使用过。

,

工作代码是:

import java.util.Scanner;

class Node {
    int data;
     Node next;
    }

public class LinkedList {

private static Node start;

public Node create(Node start) {
    Scanner sc = new Scanner(System.in);
    Node node = new Node();
    System.out.println("Enter Details:");
    node.data = sc.nextInt();
    node.next = null;
    start = node;
    System.out.println("Do you want to continue(Y/N)?");
    char ch = sc.next().charAt(0);
    while (ch != 'n') {
        Node b = new Node();
        System.out.println("Enter Details:");
        b.data = sc.nextInt();
        b.next = null;
        node.next = b;
        node = b;
        System.out.println("Do you want to continue(Y/N)?");
        ch = sc.next().charAt(0);
    }
    sc.close();
    return node;
}

public void display(Node start) {
    Node temp = start;
    while (temp != null) {
        System.out.println(temp.data);
        temp = temp.next;
    }
}

public static void main(String[] args) {
    LinkedList linkedList = new LinkedList();
    Node node = linkedList.create(start);
    linkedList.display(node);
}
}
,

start 节点传递给 create 方法不会使其成为列表的头节点。它仍然是 null。 有很多方法可以实现您想要的目标,例如:

  1. create 方法内部的列表显示为:
while(ch!='n')
{ /* ... */ }
display(start);
sc.close();
  1. 更改方法返回类型,使其返回可用于遍历列表的start(头)节点:
public static Node create(Node start)
{
    // Creating list
    return start;
}

方法调用:

public static void main(String[] args)
{
    Node start=null;
    start=create(start);
    display(start);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。