创建循环链表带范型CicleLinkedList
public class CicleLinkedList {
private Node head;
private Node tail;
public CicleLinkedList() {
tail = head = null;
}
public void addNode(T data){
if(head == null){
head = new Node(data);
tail = head;
tail.next = head;
return;
}
tail.next = new Node(data);
tail = tail.next;//修改引用指向;
tail.next = head;//构成循环;
}
//打印循环结果
public void print(){
Node curNode = head;
while(curNode.next!=head){
System.out.print(curNode.data+" ");
curNode = curNode.next;
}
System.out.println(curNode.data);
}
}