基本思想:
- 環(huán)形展開(kāi)成鏈表琅关,在鏈表上模擬環(huán)形隊(duì)列;
- head 和tail只增不減讥蔽,add 涣易、remove、size都很好理解冶伞;
- 初始容量是2的n次方新症; PS,優(yōu)秀的數(shù)據(jù)結(jié)構(gòu)肯定是結(jié)構(gòu)和數(shù)據(jù)都很巧妙的~
public class CirQueue {
Integer[] container;
//head指向next坑位
int head = 0;
//tail指向最后一個(gè)有效坑位
int tail = -1;
int size = 4;
public CirQueue() {
//初始化size最好是2^n响禽,當(dāng)head增長(zhǎng)到特比大的時(shí)候徒爹,head和tail一塊右移
this.container = new Integer[4];
}
public boolean add(Integer x) {
if (size() >= size) {
return false;
}
//因?yàn)閟ize=2^n,所以與操作最方便了
//tail和head只增不減芋类,但是你會(huì)發(fā)現(xiàn)瀑焦,tail和head的低位才真正起作用。
container[head & (size - 1)] = x;
head++;
if (tail == -1) {
//初始化梗肝,tail
tail++;
}
return true;
}
public Integer removeHead() {
if (head <= 0) {
//未開(kāi)始
return null;
}
if (head <= tail) {
//已結(jié)束
return null;
}
Integer t= container[(this.head - 1) & (size - 1)];
container[(this.head - 1) & (size - 1)] = null;
this.head--;
return t;
}
public Integer removeTail() {
if (tail >= head) {
return null;
}
Integer tail = container[this.tail & (size - 1)];
container[this.tail & (size - 1) & (size - 1)] = null;
this.tail++;
return tail;
}
public int size() {
return head - tail-1;
}
public static void main(String[] args) {
CirQueue cirQueue = new CirQueue();
System.out.println(cirQueue.add(1));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(2));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(3));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(4));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(5));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeTail());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(6));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(7));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(8));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(9));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(10));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.add(11));
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeTail());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeTail());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
System.out.println(cirQueue.removeHead());
System.out.println(Arrays.toString(cirQueue.container));
System.out.println(cirQueue.size());
}
}