Implementing the toString Method for Printing LinkedList

I am having problems with a project that I have for my OOP class. I'm almost done, but I still miss the toString method and the main method. Not quite sure how to do this, and would appreciate any help. I want my toString method to function as follows:

Returns a string representation of all items stored in a list. The string representation of an empty list looks like head--><--tail string representation of a non-empty list looks like this: head-->134<-->-8<-->42<-->1<--tail

 public class IntegerNode{ private IntegerNode next; private IntegerNode prev; private int data; public IntegerNode(int data){ next = next; prev = prev; data = data; } public int getData(){ data = data; return this.data; } public IntegerNode getNext(){ return next; } public IntegerNode getPrevious(){ return prev; } public void setNext(IntegerNode in){ prev = in; } public void setPrevious(IntegerNode in){ prev = in; } } 

and here's what i still have in the class IntegerLinkedList

 public class IntegerLinkedList{ private IntegerNode head; private IntegerNode tail; public IntegerLinkedList(){ head = null; tail = null; } public void addFirst(int x){ IntegerNode nH = new IntegerNode(x); if (head == null) { head = nH; tail = nH; }else{ head.setPrevious(nH); nH.setNext(head); head = nH; } } public void addLast(int x){ IntegerNode t = new IntegerNode(x); if (tail == null){ head = t; tail = t; }else{ tail.setNext(t); t.setPrevious(tail); tail = t; } } public int peekFirst(){ return head.getData(); } public int peekLast(){ return tail.getData(); } public String toString(){ if (head == null && tail == null){ String empty = "head--><--tail"; return empty; }else{ String h = "Head--> " + head; String t = tail + " <--Tail"; String m = " <--> "; // while(IntegerNode.getNext() != null) //} //return h + m + t; } } 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; } } 

I think the while loop is the way to go here, but again I'm not sure.

+5
source share
3 answers

Here's 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(); } } 

Alternatively, you can simplify the logic so as not to have an external 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(); } 
+5
source

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

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

If you want to assign data in the constructor with the same name as in the field, use the this keyword to clearly indicate what you want to assign. Also, assigning zeros next and prev does not make sense, 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; } 
+2
source

If you use Java 8+, StringJoiner simplified.

 @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 you are not using Java 8, StringBuilder is the right way.
(Runs better than directly using String )

 @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 the base for loop with the node variable to iterate through the list.


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

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

The purpose of next - next and prev - prev does not make sense.
Assigning a parameter to a field will only work if you assign the field using this. otherwise, you assign the parameter to yourself (meaningless).

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

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

Assigning data to data pointless.

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

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

Copy / paste the error. You assigned 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 a NullPointerException when polling the last value 1 from the list.
Add the missing if .

 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" means "only the remaining" value, not the "tail" value.

0
source

All Articles