为什么链表实现不起作用

huangapple go评论55阅读模式
英文:

Why is the Linked List implementation not working

问题

在你提供的Java代码中,你试图学习如何实现链表。你创建了一个在链表开头插入元素的方法,但当你通过主函数运行它时,似乎没有看到任何输出返回。

以下是你的代码的翻译部分:

class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  static ListNode head = null;

  void insertStart(int num, ListNode head)
  {
    ListNode temp = new ListNode(num);
    if (head == null)
    {
      head = temp;
    }
    else{
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head){
    ListNode temp = head;
    while (temp != null)
    {
      System.out.println(temp.data + "->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5, head);
    l1.insertStart(4, head);
    l1.insertStart(3, head);
    l1.insertStart(2, head);
    l1.insertStart(1, head);
    l1.display(head);
  }
}

请注意,你的插入方法可能存在问题,因为你传递了 head 参数,但似乎没有更新 head。这可能导致你的链表操作不按预期工作。你可以考虑修改插入方法以确保 head 在插入时正确更新。

英文:

So, im trying to learn the implementation of linked list in java. I created a method to insert in the start but when i run it through main, i don't see any output returned.

class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  static ListNode head = null;

  void insertStart(int num, ListNode head)
  {
    ListNode temp = new ListNode(num);
    if (head == null)
    {
      head = temp;
    }
    else{
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head){
    ListNode temp = head;
    while (temp != null)
    {
      System.out.println(temp.data+"->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5,head);
    l1.insertStart(4, head);
    l1.insertStart(3, head);
    l1.insertStart(2, head);
    l1.insertStart(1, head);
    l1.display(head);
  }
}

Can someone please help me, i can't seem to find the error. Everything seems perfect.

答案1

得分: 1

问题在于您将head作为参数传递给您的insert方法,因此它是一个局部变量。对局部变量的任何赋值将不会被调用者看到。

但是,退一步来说,将head作为参数传递给链表实例的方法实际上是不必要的。该实例应该知道它的头是什么。这揭示了另一个问题:head不应该是类的静态成员,而应该是一个实例变量:每个Operations的实例都应该有自己的head

这并不是问题,但insert的实现实际上不需要if...else结构。在任何情况下,都可以将temp.next赋给head,并且在任何情况下,都要设置headtemp

因此,请按以下方式更正您的代码:

class ListNode {
  int data;
  ListNode next;

  ListNode(int data) {
    this.data = data;
    this.next = null;
  }
}

class Operations {
  ListNode head = null; // 不应该是静态的!每个实例都有自己的head

  void insertStart(int num) // head不应该是参数。使用实例变量。
  {
    ListNode temp = new ListNode(num);
    // 不需要区分空链表或非空链表:
    temp.next = head;
    head = temp;
  }

  void display() { // head不应该是参数。使用实例变量。
    ListNode temp = head;
    while (temp != null) {
      System.out.print(temp.data + "->"); // 在同一行上输出
      temp = temp.next;
    }
    System.out.println("null"); // 终止行
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5); // 不要将head作为参数传递。
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display();
  }
}
英文:

The problem is that you pass head as an argument to your insert method, and so it is a local variable. Any assignment to a local variable will not be seen by the caller.

But, taking a step back, it should not be necessary to pass head as argument to a method of your linked list instance. That instance should know "itself" what its head is. And that reveals another problem: head should not be a static member of your class, but an instance variable: every instance of Operations should have its own head.

Not a problem, but the implementation of insert doesn't really need the if...else construct. In either case you can assign temp.next=head, and in either case you want to set head=temp.

So correct your code as follows:

class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  ListNode head = null; // Should not be static! Every instance has its own head

  void insertStart(int num) // head should not be an argument. Use the instance variable.
  {
    ListNode temp = new ListNode(num);
    // No distinction needed between empty list or non-empty list:
    temp.next = head;
    head = temp;
  }

  void display(){ // head should not be an argument. Use the instance variable.
    ListNode temp = head;
    while (temp != null)
    {
      System.out.print(temp.data+"->"); // Output on same line
      temp = temp.next;
    }
    System.out.println("null"); // Terminate the line
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5); // Don't pass the head as argument.
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display();
  }
}

答案2

得分: -1

以下是已翻译的代码部分:

这是你需要做的事情

class ListNode {
  int data;
  ListNode next;

  ListNode(int data) {
    this.data = data;
    this.next = null;
  }
}

class Operations {
  static ListNode head = null;

  void insertStart(int num) {
    ListNode temp = new ListNode(num);
    if (head == null) {
      head = temp;
    } else {
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head) {
    ListNode temp = head;
    while (temp != null) {
      System.out.println(temp.data + "->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5);
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display(head);
  }
}
英文:

Here is what you need to do:

    class ListNode{
  int data;
  ListNode next;

  ListNode(int data)
  {
    this.data = data;
    this.next = null;
  }
}

class Operations{
  static ListNode head = null;

  void insertStart(int num)
  {
    ListNode temp = new ListNode(num);
    if (head == null)
    {
      head = temp;
    }
    else{
      temp.next = head;
      head = temp;
    }
  }

  void display(ListNode head){
    ListNode temp = head;
    while (temp != null)
    {
      System.out.println(temp.data+"->");
      temp = temp.next;
    }
  }

  public static void main(String[] args) {
    Operations l1 = new Operations();
    l1.insertStart(5);
    l1.insertStart(4);
    l1.insertStart(3);
    l1.insertStart(2);
    l1.insertStart(1);
    l1.display(head);
  }
}

huangapple
  • 本文由 发表于 2023年4月4日 14:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定