【问题标题】:Java - Traversing a linked list only returns nullJava - 遍历链表只返回 null
【发布时间】:2014-10-08 03:38:38
【问题描述】:

我正在研究链表。在《Cracking the Coding Interview》一书中的帮助下,我创建了以下代码来创建链表,将元素添加到其末尾并打印元素。但是,当我运行代码时,它只返回“null”而不是打印列表,即“Sanchez”。 “厄齐尔”和“维尔贝克”。帮忙?

public class CreateLinkedList{

    static class Node{
        String PlayerName;
        Node next = null;

        //Constructor
        Node(String PName){
            PlayerName = PName;
        }

        //Method to insert a Node
        void InsertNodeAtEnd(String PlayerName){
            Node transition = new Node(PlayerName);
            Node n = this;
            while(n.next != null){
                n = n.next;
                }
            n.next = transition;            
            }       

        //Method to print all elements of linked list
        void PrintList(){
            Node n = this;
            while (n.next != null){
                System.out.println(n.PlayerName + "\n");
                n = n.next;
            }
        }
    }

    public static void main(String[] args) {

        Node first = new Node("Sanchez");
        first.InsertNodeAtEnd("Ozil");
        first.InsertNodeAtEnd("Welbeck");
        first.PrintList();
    }   
}

【问题讨论】:

  • 我只是按照目前的方式运行您的代码,我得到了SanchezOzil 作为输出(仍然缺少一个,但请参阅@Debasish Jana 的答案)。你在哪里得到空值?
  • 你刚刚发现我的 Eclipse 出了点问题。我知道很奇怪。在 netbeans 中试过,效果很好

标签: java class methods linked-list system.out


【解决方案1】:
public class CreateLinkedList {
static class Node {
    String PlayerName;
    Node next = null;

    // Constructor
    Node(String PName) {
        PlayerName = PName;
    }

    // Method to insert a Node
    void InsertNodeAtEnd(String PlayerName) {
        Node transition = new Node(PlayerName);
        Node n = this;
        while (n.next != null) {
            n = n.next;
        }
        n.next = transition;
    }

    // Method to print all elements of linked list
    void PrintList() {
        Node n = this;
        while (n != null) {
            System.out.println(n.PlayerName + "\n");
            n = n.next;
        }
    }
}

public static void main(String[] args) {

    Node first = new Node("Sanchez");
    first.InsertNodeAtEnd("Ozil");
    first.InsertNodeAtEnd("Welbeck");
    first.PrintList();
}
}

输出

Sanchez

Ozil

Welbeck

【讨论】:

    【解决方案2】:

    在PrintList中,对每个元素进行迭代,最后一次迭代丢失了,所以我改变了循环条件

    void PrintList(){
                    Node n = this;
                    while (n != null){
                        System.out.println(n.PlayerName + "\n");
                        n = n.next;
                    }
                }
    

    【讨论】:

      【解决方案3】:

      我很确定问题出在您添加到链接列表时。

      Node n = this;
      

      this 指向类本身,它不是节点。
      您应该在代表第一个节点的类中实现一个节点变量。所以是这样的:

      public class CreateLinkedList{
      
      
      static class Node{
          //all this stuff seemed fine
          }
      
      Node first = null;//represents first item
      
          //Method to insert a Node
          void InsertNodeAtEnd(String PlayerName){
              Node transition = new Node(PlayerName);
              Node n = first;
             if(n!=null){
                  while(n.next != null){
                     n = n.next;
                     }
                 n.next = transition;            
              }
             else
               first = transition;
      
      
      
          //Method to print all elements of linked list
          void PrintList(){
              Node n = first;
              while (n.next != null){
                  System.out.println(n.PlayerName + "\n");
                  n = n.next;
              }
          }
      }
      
      public static void main(String[] args) {
      
          Node first = new Node("Sanchez");
          first.InsertNodeAtEnd("Ozil");
          first.InsertNodeAtEnd("Welbeck");
          first.PrintList();
      }   
      }'
      

      【讨论】:

        【解决方案4】:

        在 printList 中迭代时使用以下作为检查条件:

        n!=null

        而不是

        n.next != null

        在您的代码中,当您比较下一个节点而不是当前节点时,循环会跳过最后一次迭代。

        正确的方法是:

        //Method to print all elements of linked list
         void PrintList(){
            Node n = this;
            while (n != null){
                System.out.println(n.PlayerName + "\n");
                n = n.next;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-01-05
          • 2022-12-04
          • 1970-01-01
          • 1970-01-01
          • 2021-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多