// 循环链表
public class CircleLinkList {
// 节点
class Link {
int data;
Link next;
public Link(int temp) {
data = temp;
next = null;
}
public void display() {
System.out.print(data+" ");
}
}
// 当前节点
Link current = null;
// 当前节点的前一个节点
Link prev = null;
// 节点个数
int size = 0;
// 判断是否为空
public boolean isEmpty() {
return (size == 0);
}
// 遍历链表
public void display() {
if (!isEmpty()) {
int count = 0;
while ((count++) < size) {
current.display();
nextLink();
}
}
else
System.out.println("数据为空!");
}
// 获取当前节点
public Integer getCurrent() {
return isEmpty() ? null : current.data;
}
// 节点移动一个位置
public boolean nextLink() {
return (prev == null) ? false : ((current = (prev = current).next) != null);
}
// 获取节点个数
public int Size() {
return size;
}
// 插入节点
public void insertAfter(int a) {
Link newLink = new Link(a);
if (isEmpty()) {
current = newLink;
size = 1;
return;
}
if (prev == null)
(prev = newLink).next = current;
else
newLink.next = current.next;
current.next = newLink;
nextLink();
size++;
}
// 删除当前节点
public Integer deleteCurrent() {
if (isEmpty())
return null;
int temp = current.data;
if (current.next != null)
if (current.next == prev)
prev = (prev.next = null);
else
prev.next = current.next;
current = current.next;
size--;
return temp;
}
// 查找某节点
public boolean find(int temp) {
int count = 0;
while ((count++) <= size) {
if (current.data == temp)
return true;
nextLink();
}
return false;
}
}