Displaying a linked list in java -


i'm working on assignment need create linked list given template. each new node created, need print out updated list. however, until point, i've been stumped on how print out linked list. can figure out doing wrong? have prints out number has been created, followed blank space, instead of entire list 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();          (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();          (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();          (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 + "";   }  } 

i think tostring() looping endlessly once second element added. need move pointer next node:

public string tostring() {       node tmp = head;        string result = "";       while (tmp != null) {           result += tmp.tostring() + " ";           tmp = tmp.getnext();       }        return result;   } 

i've updated condition handle null head.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -