1.继承自双向链表,然后把双向链表的尾巴的end的next指向head。然后它的Insert,delete,get,函数不变。
public static void main(String[] args) throws IOException {
int[] data = new int[4];
data[0] = 1;
data[1] = 2;
data[2] = 3;
data[3] = 4;
//定义一个统计表
IntCircleTable table = new IntCircleTable(data[0]);
//将数据加载到表中
for (int i =1;i<data.length;i++){
table.add(new IntDoubleDirElement(data[i]));
}
//遍历统计
String value = "";
for (int i =0;i<30;i++){
value+= table.get(i)+" ";
}
System.out.println(value);
//插入测试
table.insert(new IntDoubleDirElement(91),2);
value = "";
for (int i =0;i<30;i++){
value+= table.get(i)+" ";
}
System.out.println(value);
//删除测试
table.delete(1);
value = "";
for (int i =0;i<30;i++){
value+= table.get(i)+" ";
}
System.out.println(value);
}
2.实现:
package com.table.circle;
import DoubleDirTable.IntDoubleDirElement;
import DoubleDirTable.IntDoubleDirTable;
public class IntCircleTable extends IntDoubleDirTable {
public IntCircleTable(int value) {
super(value);
}
@Override
public void add(IntDoubleDirElement intDoubleDirElement) {
end().setNext(intDoubleDirElement);
intDoubleDirElement.setLast(end());
setEnd(intDoubleDirElement);
end().setNext(head());
}
}