- first you need to fix Number constructor. You should assign head to first node and tail to last node
public Number(int n) { number = n; head = new Node(0); Node prev = head; for (int i = 1; i <=number; i++) { Node node = new Node(i); prev.next=node; // head I will have 1, 2, 3... n prev=node; } tail=prev; }
2 . And in toString method you were always referring head.next which was causing infiinte loop as you never hit null (current != null will never become true in your code)
public String toString() { String strVal = ""; for (Node current = head; current != null; current = current.next) { strVal = strVal +" "+ current.num; } return strVal; }
edit: One thing I would like to add is, your implemetation is of plain linked list and not circular linked list.. To make it circular, you should point tail to head,tail=head