java – Implementing a toString method to print out a LinkedList

java – Implementing a toString method to print out a LinkedList

Heres how to write it:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
    if (head == null && tail == null){
        String empty = head--><--tail;
        return empty;
    }else{
       StringBuilder sb = new StringBuilder();
       sb.append(Head-->);

       IntegerNode curr = head;
       sb.append(curr.getData());
       curr = curr.getNext();
       while(curr != null) {
           sb.append(<-->);
           sb.append(curr.getData());
           curr = curr.getNext();
       }
       sb.append(<--tail);
       return sb.toString();
    }
}

As an alternative, you can simplify the logic to not have an outer if else:

@Override  // <-- Annotate that you are overriding the toString() method
public String toString(){
   StringBuilder sb = new StringBuilder();
   sb.append(Head-->);

   IntegerNode curr = head;

   if (curr == null)
   {
      sb.append(<--tail);
      return sb.toString();
   }

   sb.append(curr.getData());
   curr = curr.getNext();
   while(curr != null) {
       sb.append(<-->);
       sb.append(curr.getData());
       curr = curr.getNext();
   }
   sb.append(<--tail);

   return sb.toString();
}

Yes, you have to use a loop, because you want to iterate over data of unknown length. Michael Markidis wrote the answer more quickly than me, use his solution, however I would suggest some improvements to your code.

String h = Head--> + head; wont work, because head is an object IntegerNode, and you want to access its data like this head.getData() (also why do you assign data = data; in this method? It should only do return)

If you want to assign data in constructor that have the same name as field, use have to use this keyword to make clear what do you want to assign. Also assigning null next and prev has no meaning, so this code

public IntegerNode(int data){
    next = next; 
    prev = prev;
    data = data;     
}

should look like this

public IntegerNode(int data){
    this.data = data;     
}

or if you want to assign the previous and next node

public IntegerNode(int data, IntegerNode next, IntegerNode prev){
    this.next = next; 
    this.prev = prev;
    this.data = data;     
}

java – Implementing a toString method to print out a LinkedList

If you are using Java 8+, StringJoiner makes it easy.

@Override
public String toString() {
    StringJoiner joiner = new StringJoiner(<-->, head-->, <--tail);
    for (IntegerNode node = this.head; node != null; node = node.getNext())
        joiner.add(String.valueOf(node.getData()));
    return joiner.toString();
}

If not using Java 8, a StringBuilder is the right way to go.
  (Performs better than using String directly)

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(head-->);
    boolean sep = false;
    for (IntegerNode node = this.head; node != null; node = node.getNext()) {
        if (sep)
            buf.append(<-->);
        buf.append(node.getData());
        sep = true;
    }
    return buf.append(<--tail).toString();
}

In both cases, you use a basic for loop with a node variable to iterate through the list.


As for the rest of your code, you have some issues.

public IntegerNode(int data){
    next = next; 
    prev = prev;
    data = data;     
}

Assigning next to next and prev to prev is meaningless.
Assigning the parameter to the field will only work if you qualify the field with this., otherwise youre assigning the parameter to itself (meaningless).

public IntegerNode(int data){
    this.data = data;     
}

public int getData(){
    data = data;
    return this.data;   
}

Assigning data to data is meaningless.

public int getData(){
    return this.data;   
}

public void setNext(IntegerNode in){
    prev = in;
}

Copy/paste error. You meant to assign to next.

public void setNext(IntegerNode in){
    next = in;
}

public int pollFirst(){
    int x = head.getData();
    head = head.getNext();
    head.setPrevious(null);
    return x;
}

public int pollLast(){
    int x = tail.getData();
    tail = tail.getPrevious();
    tail.setNext(null);
    return x;
}

These methods will throw NullPointerException when you poll the last1 value from the list.
Add missing if statement.

public int pollFirst(){
    int x = head.getData();
    head = head.getNext();
    if (head == null)
        tail = null;
    else
        head.setPrevious(null);
    return x;
}

public int pollLast(){
    int x = tail.getData();
    tail = tail.getPrevious();
    if (tail == null)
        head = null;
    else
        tail.setNext(null);
    return x;
}

1) last refers to the only remaining value, not the tail value.

Leave a Reply

Your email address will not be published. Required fields are marked *