一.循环列表的组成
1.一个头指针,一个尾指针,一个数组存放数据
2.在Java里面我们时常要有对象概念,所以循环队列里面还具有相应的操作方法,增、删、查、改、打印等等。
二.循环队列的作用
循环不就是兜圈圈嘛?有啥作用呢?最主要的作用相对于链表而言就是空间利用率高。
单队列如下图,很多空间浪费了,循环队列形成环,可以利用之前出队的空间。
此时判断队列为空和满时,就不能简单的rear = =front,多剩一个空间。
入队:
base[rear]=e;
rear=(rear+1)%M;
出队:
e=base[front];
front=(front+1)%M;
队空:front == rear
队满:(rear+1) % M == front
代码展示:一定要细看,里面有注释,不懂品论区就问注意判断条件以及front,rear操作
入队:
public void enqueue(int paraValue) {
if ((rear + 1) % TOTAL_SPACE == front) {
System.out.println("Queue full.");
return;
} // Of if
data[rear % TOTAL_SPACE] = paraValue;
rear = (rear + 1) % TOTAL_SPACE;
}// Of enqueue
出队:
public int dequeue() {
if(rear==front) {
System.out.println("No element in the queue");
return -1;
}// Of if
int resultValue = data[front%TOTAL_SPACE];
front=(front+1)%TOTAL_SPACE;
return resultValue;
}// Of dequeue
toString重写:
public String toString() {
String resulString = "";
if(rear==front) {
return "empty";
}
//设置遍历标记
int rear1=rear,front1=front;
while(rear1!=front1) {
resulString+=data[front1%TOTAL_SPACE]+",";
front1=(front1+1)%TOTAL_SPACE;
}
return resulString;
}
总代码:
/**
*
*/
package datastructure.queue;
import javax.naming.spi.DirStateFactory.Result;
/**
****************************
* TODO
* @author Chen Fan
* @version 1.0
* time 2021年12月26日
****************************
*/
public class CircleIntQueue {
public static final int TOTAL_SPACE = 10;
int[] data;
int front;
int rear;
//构造函数,构造循环队列
public CircleIntQueue() {
data = new int[TOTAL_SPACE];
front = 0;
rear = 0;
}// Of construct
//入队操作
public void enqueue(int paraValue) {
if ((rear + 1) % TOTAL_SPACE == front) {
System.out.println("Queue full.");
return;
} // Of if
data[rear % TOTAL_SPACE] = paraValue;
rear = (rear + 1) % TOTAL_SPACE;
}// Of enqueue
// 出队
public int dequeue() {
if(rear==front) {
System.out.println("No element in the queue");
return -1;
}// Of if
int resultValue = data[front%TOTAL_SPACE];
front=(front+1)%TOTAL_SPACE;
return resultValue;
}// Of dequeue
//重写函数
public String toString() {
String resulString = "";
if(rear==front) {
return "empty";
}
//设置遍历标记
int rear1=rear,front1=front;
while(rear1!=front1) {
resulString+=data[front1%TOTAL_SPACE]+",";
front1=(front1+1)%TOTAL_SPACE;
}
return resulString;
}
//测试函数
public static void main(String args[]) {
CircleIntQueue tempQueue = new CircleIntQueue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 1);
} // Of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
int tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 10);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 100);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
} // Of for i
}// Of main
}