Leetcode学习队列篇
题目地址: https://leetcode-cn.com/leetbook/read/queue-stack/kzlb5/
多年没有写代码了,语言都忘差不多了。就这一道题,我花费了两天啊,总算做出来了,结果还不错,纪念一下。庆幸自己还有救,不能放弃治疗。全面恢复代码功力中,加油!
show code
class MyCircularQueue {
private int head;
private int tail;
private int[] data;
private int len;
private int count;
public MyCircularQueue(int k) {
head = 0;
tail = 0;
len = k;
count = 0;
data = new int[k];
}
public boolean enQueue(int value) {
if(isFull()) return false;
else{
data[tail] = value;
tail=(tail+1)%len;
count++;
return true;
}
}
public boolean deQueue() {
if(isEmpty()) return false;
else{
head=(head+1)%len;
count--;
return true;
}
}
public int Front() {
if(isEmpty()) return -1;
else{
return data[head];
}
}
public int Rear() {
if(isEmpty()) return -1;
else return data[(tail+len-1)%len];
}
public boolean isEmpty() {
if( count==0)
return true;
else
return false;
}
public boolean isFull() {
if(count == len)
return true;
else
return false;
}
}