所以基本上我正在制作一个循环链表,当我尝试打印出我的代码时,Eclipse 的控制台上没有任何内容被打印出来。我希望所有 6 个数字都显示在控制台上。如果可以的话,请帮助我。非常感谢!
public class CircularLinkedList
{
public Node head;
public Node tail;
public CircularLinkedList()
{
head =null;
tail =null;
}
public void insert(int v)
{
if( head == null)
{
head = new Node(v, null);
tail = head;
}
else
{
Node newNode = new Node(v,head);
tail.setNextNode(newNode);
tail = newNode;
}
}
public String toString()
{
if( head == null)
{
System.out.println("The list is empty");
}
Node newTemp = head;
String result = "";
while(newTemp != head)
{
result += newTemp.toString();
newTemp = newTemp.getNextNode();
}
return result;
}
public static void main(String[] args)
{
CircularLinkedList a = new CircularLinkedList();
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(4);
a.insert(5);
a.insert(6);
System.out.println(a.toString());
}
}