I'm doing my assignment which is to create a Circular List, I believe that my methods are working properly, except for my print method.
public String print() {
String str = "";
Node curr = first;
for(int i = 0; i < getSize(); curr = curr.getNext(), i++) {
str += curr.getData();
}
return str;
}
I test it with the numbers: {1,2,3,4,5}, and when I call my print method I get 15432 as a result. Does any one know what could be wrong with this, apparently I'm not seeing it. Also if you need more code, let me know.
EDIT: After some comments I realize it's not my print method, here are my add() and getData() methods:
public void add(int value) {
if(first == null) {
first = new Node(value, null);
first.setNext(first);
} else {
Node newNode = new Node(value, first.getNext());
first.setNext(newNode);
}
System.out.println("Added: " + value);
size++;
}
public int getData() {
return first.getData();
}
EDIT 2: The constructor;
class CircularList {
private Node first;
private int size;
public CircularList() {
first = null;
size = 0;
}
....