循环队列
1.循环队列的基本认识
2.具体实现
- 用数组实现循环队列
2.1入队
- 思路:
- 同样判断是否为满
- 添加的元素加在rear的位置,然后rear向后面移动,本来只需要加加,但是是循环队列,最后一个的下一个是0号下标,因此用模运算;
//入队
public boolean enQueue(int value) {
//1.先判断是否为满
if(isFull()){
return false;
}
this.elem[rear] = value;
rear = (rear+1)%this.elem.length;
return true;
}
2.2出队
- 思路:
- 首先判断是否为空
- 直接从队头front出;
public boolean deQueue() {
//1.判断是否为空
if(isEmpty()){
return false;
}
front = (front+1)%elem.length;
return true;
}
2.3获取队头元素
- 思路:因为是用数组实现,只要直接返回队头下标的元素就可以;
public int Front() {
//1.判断是否为空
if(isEmpty()){
return -1;
}
return elem[front];
}
2.4获取队尾元素
- 思路:如果rear在0号位置返回上一个需要模运算;负责直接减减就可以;
//获取队尾
public int Rear() {
//1.判断是否为空
if(isEmpty()){
return -1;
}
int index = this.rear==0 ? this.elem.length-1 : this.rear-1;
return elem[index];
}
2.5判断是否为空
- 思路:只要rear == front就为空
//是否为空
public boolean isEmpty() {
if(rear == front){
return true;
}
return false;
}
2.6判断是否为满
- 思路:只要判断rear的下一个是否为队头元素;
public boolean isFull() {
if((rear+1)%elem.length == front){
return true;
}
return false;
}
3.完整代码
//循环队列(用数组实现)
class MyCircularQueue {
public int[] elem;
public int front;//队头
public int rear;//队尾
/** Initialize your data structure here. Set the size of the queue to be k. */
//构造方法
public MyCircularQueue(int k) {
this.elem = new int[k+1];
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
//入队
public boolean enQueue(int value) {
//1.先判断是否为满
if(isFull()){
return false;
}
this.elem[rear] = value;
rear = (rear+1)%this.elem.length;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
//出队
public boolean deQueue() {
//1.判断是否为空
if(isEmpty()){
return false;
}
front = (front+1)%elem.length;
return true;
}
/** Get the front item from the queue. */
//获取队头元素
public int Front() {
//1.判断是否为空
if(isEmpty()){
return -1;
}
return elem[front];
}
/** Get the last item from the queue. */
//获取队尾
public int Rear() {
//1.判断是否为空
if(isEmpty()){
return -1;
}
int index = this.rear==0 ? this.elem.length-1 : this.rear-1;
return elem[index];
}
/** Checks whether the circular queue is empty or not. */
//是否为空
public boolean isEmpty() {
if(rear == front){
return true;
}
return false;
}
/** Checks whether the circular queue is full or not. */
//是否为满
public boolean isFull() {
if((rear+1)%elem.length == front){
return true;
}
return false;
}
}
public class TestDemo4 {
public static void main(String[] args) {
MyCircularQueue myCircularQueue = new MyCircularQueue(8);
myCircularQueue.enQueue(1);
myCircularQueue.enQueue(2);
myCircularQueue.enQueue(3);
myCircularQueue.enQueue(4);
System.out.println(myCircularQueue.Front());
System.out.println(myCircularQueue.Rear());
}
}