队列:
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
/**
* 先进先出
*/
class MyCircularQueue {
private int[] data;
private int size;
private int head; //头指针
private int tail; //尾指针
public MyCircularQueue(int k) {
this.data = new int[k];
this.size = 0;
this.head = -1;
this.tail = -1;
}
public boolean enQueue(int value) {
if (size == data.length){
return false;
}
tail = (tail + 1)%data.length;
if (size == 0){
head = tail;
}
size ++;
data[tail] = value;
return true;
}
public boolean deQueue() {
if (size == 0){
return false;
}
head = (head + 1)%data.length;
size --;
return true;
}
public int Front() {
if(size == 0){
return -1;
}
return data[head];
}
public int Rear() {
if(size == 0){
return -1;
}
return data[tail];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == data.length;
}
}