Jason Khalili
Jason Khalili

Reputation: 101

Printing a Linked List in Java

I'm working on an assignment where I need to create a Linked List given a template. 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?

Edit: Sorry, I should point out that I'm getting a java.lang.NullPointerException error on line 27 of NumberList when I compile.

error when I compile.

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: 111

Answers (2)

user6026234
user6026234

Reputation:

You're calling insertAtTail and insertInOrder methods, but they are empty. As well as your NumberList constructor.

Upvotes: 0

Idos
Idos

Reputation: 15310

Your problem is:

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

You are not advancing to the next link in your list at all. You should consider performing
tmp = tmp.getNext(). But before doing so make sure your while condition is
while (tmp != null) to avoid a NullPointerException.

And the NullPointerException you are getting in line 27 (which we can't know where it is) is probably because head isn't initialized since you call insertInOrder before insertAtHead, which is the only place where head is initialized in your program.

Upvotes: 2

Related Questions