【问题标题】:Printing elements of a linked list in java在java中打印链表的元素
【发布时间】:2018-12-16 18:25:51
【问题描述】:

我正在尝试在 java 中实现链表。在我的主类中,我从用户那里得到一些整数并将它们放在一个链表中,然后打印出我的链表元素。到目前为止一切正常,但是我认为在我的主课中,首先打印出每个元素的数据然后继续下一个元素是有意义的。当我这样做时,它不会打印我列表的最后一个元素,但它会打印第一个元素两次。我决定先移动到下一个元素,然后打印前一个元素的数据,它工作得很好!!!谁能解释一下原因?(查看我的代码的最后两行)。

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

我的链表类:

public class LinkedList {
Node head;

public void append(int data){
    if(head==null){
        head=new Node(data);
    }
    Node current;
    current=head;
    while(current.next!=null){
        current=current.next;
    }
    current.next=new Node(data);
}
}

我的主要课程:

public class Main {
static LinkedList linkedList =new LinkedList();
public static void main(String [] args){
    System.out.println("please enter numbers you wanna store in a linked list");
    Scanner scanner=new Scanner(System.in);
    while (scanner.hasNextInt()){
        linkedList.append(scanner.nextInt());
    }
    if (linkedList.head!=null){
        Node current;
        current=linkedList.head;
        while (current.next!=null){
            **current=current.next;
            System.out.println(current.data);**
        }
    }
}
}

【问题讨论】:

    标签: java singly-linked-list


    【解决方案1】:

    在您的 append 语句中,您将第一个元素添加了两次:

    public void append(int data){
        if(head==null){
            head=new Node(data); // <--- Added here
        }
        Node current;
        current=head;
        while(current.next!=null){
            current=current.next;
        }
        current.next=new Node(data); // <--- And then again here
    }
    

    所以你的链表实际上包含了第一个元素两次。之后不会发生,因为列表中已经有一个头部。

    你应该在head = new Node(data)之后添加一个return,或者有一个else语句:

    public void append(int data){
        if(head==null){
            head=new Node(data);
        } else {
            Node current;
            current=head;
            while(current.next!=null){
                current=current.next;
            }
            current.next=new Node(data);
        }
    }
    

    【讨论】:

    • 这仅处理两个错误之一。
    • @DawoodibnKareem 如果你的意思是打印语句中的额外下一个,那么 OP 说这是一个“变通办法”,所以它最初不存在。
    • 不,我完全不是这个意思。
    【解决方案2】:

    你有两个错误。

    (1) 您将第一个元素添加两次。你有

    if(head==null){
        head=new Node(data);
    } 
    

    然后你继续并再次添加它。

    (2) 当您打印出列表时,您将在current.next == null 时停止 - 因此您在到达最后一个元素之前就停止了。 while 循环的条件需要是

    while(current != null) {
    

    而不是检查current.next

    【讨论】:

      【解决方案3】:

      交换这两个语句。在进入下一个节点之前打印数据:

      System.out.println(current.data);
      current=current.next;
      

      并将 while 条件从 current.next!=null 更改为 while current!=null,因为 current.next() 对于最后一个节点将为空,因此不会被打印

      您还在 append 方法中添加了两次第一个元素。改成下面这样:

      public void append(int data){
          if(head==null){
              head=new Node(data);
          }
      else{
          Node current;
          current=head;
          while(current.next!=null){
              current=current.next;
          }
          current.next=new Node(data);}
      }
      

      【讨论】:

      • 当我这样做时,它不会打印我列表的最后一个元素,但它会打印第一个元素两次。我想知道为什么,这正是我的问题。
      • @backslashN:在纸上处理你的代码,你会看到方法。这只是简单的逻辑。
      • 正如 RealSpectic 正确提到的,您在 append 方法中添加了两次元素。
      • 这不能回答问题。您的建议是 OP 最初的原因,导致了问题。
      猜你喜欢
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多