简体   繁体   English

在Java中显示链表

[英]Displaying a linked list in java

I'm working on an assignment where I need to create a Linked List given a template. 我正在做一个作业,需要在给定模板的情况下创建链接列表。 For each new node that is created, I need to print out the updated list. 对于创建的每个新节点,我需要打印出更新后的列表。 However, up until this point, I've been stumped on how to print out the linked list. 但是,到目前为止,我一直对如何打印出链表感到困惑。 Can anyone figure out what am I doing wrong? 谁能知道我在做什么错? What I currently have just prints out the number that has been created, followed by blank space, instead of the entire list up to that point. 我目前刚刚打印出的数字已经创建,然后是空格,而不是整个列表。

NumberList.java NumberList.java

import java.util.*;

public class NumberList {

  private Node head;

  public NumberList() {
  }
  public void insertAtHead(int x) {
      Node newNode = new Node(x);

      if (head == null)
          head = newNode;
      else {
          newNode.setNext(head);
          head = newNode;
      }
  }
  public void insertAtTail(int x) {
  }
  public void insertInOrder(int x) {
  }
  public String toString() {
      Node tmp = head;

      String result = "";
      while (tmp.getNext() != null) {
          result += tmp.toString() + " ";
      }

      return result;
  }
  //---------------------

  // test methods

  //---------------------

  public static void testInsertAtHead() {

        Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertAtHead(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void testInsertAtTail() {

        Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertAtTail(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void testInsertInOrder() {

      Random r = new Random();
        int n = 20;
        int range = 1000;

        NumberList list = new NumberList();

        for (int i=1; i<=n; i++) {
              int x = r.nextInt(range);
              list.insertInOrder(x);
              System.out.println("" + x + ": " + list);
        }
  }

  public static void main(String[] args) {
        //testInsertAtHead();
        //testInsertAtTail();
        testInsertInOrder();
  }
}

Node.java Node.java

class Node {

  private int number;
  private Node next;

  public Node(int n) {
     this.number = n;
     this.next = null;
  }
  public Node getNext() {
      return next;
  }
  public int getNumber() {
      return number;
  }
  public void setNext(Node n) {
      if (n == null)
          return;

      n.setNext(next);
      next = n;
  }
  public String toString() {
      return number + "";
  }

}

I think your toString() is looping endlessly once the second element is added. 我认为一旦添加第二个元素,您的toString()就会无限循环。 You need to move your pointer to the next node: 您需要将指针移动到下一个节点:

public String toString() {
      Node tmp = head;

      String result = "";
      while (tmp != null) {
          result += tmp.toString() + " ";
          tmp = tmp.getNext();
      }

      return result;
  }

I've also updated the condition to handle a null head . 我还更新来处理一个空的条件head

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM