如何在不使用内置函数的情况下获取 LinkedList class 的下一个指针?

How can I get my next pointer of my LinkedList class without using built in function?

我是编程新手,正在练习 Java 编程语言。我在寻找我的程序的解决方案时度过了艰难的一天,因为我无法获得我的“下一个”指针并且我真的想打印我的最后一个值。有人可以帮我解决这个问题并向我解释吗?先感谢您。这是我的代码。

注意:我程序的输出是5.

public class Node {

private int data;
private Node next;

public Node (int data){
    this.data = data;
}

public int getData() {
    return this.data;
}

public void setNext(Node n) {
    this.next = n;
}

public Node getNext() {
    return this.next;
}
}

public class LinkedList {

private static Node head, next;

public LinkedList (int data) {
    head = new Node (data);
}

public void addLast(int data) {
    Node n = new Node (data);

    if (head == null) {
        head = n;
    }
    else {
        Node temp = head;
        temp.setNext(next);
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        Node t = temp.getNext();
        t = n;
    }
}

public void printList() {

    head.setNext(next);

    while (head.getNext() != null) {
        System.out.println(head.getData());
        head = head.getNext();
    }
    System.out.println(head.getData());
}

public static void main(String[] args) {

    LinkedList l = new LinkedList(5);
    l.addLast(7);
    l.printList();
}
}

TL;DR:

  1. 您将 null 设置为 printList() 方法中的下一个节点;
  2. 你的addLast也不行(你没有设置下一个节点(详见下文);
  3. 您永远不应该在 print 方法中设置节点(或进行任何逻辑更改)。 Print 顾名思义就是打印,不应该包含任何side-effect,修改你的数据结构。那就是:你必须清楚地分开你的关注点。

在您当前的 addLast 中,您:

public void addLast(int data) {
    Node n = new Node (data);

    if (head == null) {
        head = n;
    }
    else {
        Node temp = head;
        temp.setNext(next);
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        Node t = temp.getNext();
        t = n;
    }
}

这意味着,当您的 temp 的下一个节点是 null 时,您永远不会添加您使用 int 参数实例化的节点。

改变else块如下:

else {
    Node temp = head;
    temp.setNext(next);
    while (temp.getNext() != null) {
        temp = temp.getNext();
    }
    temp.setNext(n);
    //two redundant lines removed
}

相应地,从您的 printList() 方法中删除 head.setNext(next);(可能还有不必要的 System.out.println() 语句)。


P. S. 我真的建议你花一些时间在 Linked List Data Structure (Data Structure, 而不是 Java code ), 作为你现在的设计,说明你需要更好地掌握它。

我建议对您的代码进行以下两项修改。

  1. 在 addLast 方法中使用以下 else 块。

         Node temp = head;
         temp.setNext(next); // this line causing the next object to be set to null all the time. commenting this line will help in making sure the follwing loop reaches to end of the list, otherwise the while loop will always exit without any iteration.
         while (temp.getNext() != null) {
             temp = temp.getNext();
         }
         Node t = temp.getNext(); 
         t = n; // this will also not change the linking. Its basically assigned a new value to t.
    

使用以下建议

        Node temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        // now we reached end of list and temp.next is null.
        // assign newly createdd node to temp.next
        temp.setNext(n);
  1. 在迭代 printList 中的元素时,存在第 1 点中提到的相同问题。请尝试对 printList 方法使用以下建议。

      //  head.setNext(next); // This line will always set head.next to null and whole list will be lost. Instead of this use following line
       Node temp = head;
        while (temp.getNext() != null) { // here if you use head its position will move to end. So use temp variable for iteration
            System.out.println(temp.getData());
            temp= temp.getNext();
        }
       System.out.println(temp.getData());

您可能还需要学习列表迭代算法才能更好地理解。

我做了一些修改以使您的代码正常工作。您的 addLast 方法中的 If 语句:

if (head == null) {

是多余的,因为你的 LinkedList 只能通过传递一些 data 来初始化,因此 head 永远不会是 null,它总是指向包含 data

Node

也行

head.setNext(next);

在你的 printList() 中有问题,它总是指向 null

public class LinkedList {

  private static Node head, next;

  public LinkedList(int data) {
    head = new Node(data);
  }

  public void addLast(int data) {
    Node n = new Node(data);
    Node temp = head;
    temp.setNext(next);
    while (temp.getNext() != null) {
      temp = temp.getNext();
    }
    temp.setNext(n);
  }

  public void printList() {
    while (head.getNext() != null) {
      System.out.println(head.getData());
      head = head.getNext();
    }
    System.out.println(head.getData());
  }

  public static void main(String[] args) {
    LinkedList l = new LinkedList(5);
    l.addLast(7);
    l.printList();
  }
}