jkaleel
jkaleel

Reputation: 13

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

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

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 + "";
  }

}

Upvotes: 0

Views: 303

Answers (1)

shmosel
shmosel

Reputation: 50716

I think your toString() is looping endlessly once the second element is added. 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.

Upvotes: 1

Related Questions