
01_概念

02_特点

03_分类

04_基本运算

05_Java实现

06_Java实现(单向队列)
package DataStructureReview;
public class ArrayQueueDemo {
public static void main(String[] args) {
//创建队列对象
ArrayQueue queue = new ArrayQueue(4);
//判断空队
System.out.println("是否空队:"+queue.isEmpty());
//判断满队
System.out.println("是否满队:"+queue.isFull());
//元素进队
queue.inQueue(1);
queue.inQueue(2);
queue.inQueue(3);
queue.inQueue(4);
//queue.inQueue(5);
System.out.println("front元素为:"+queue.getFront());
queue.printQueue();
System.out.printf("%d 出队\n",queue.outQueue());
//"假溢出"现象
//queue.inQueue(5);
}
}
class ArrayQueue {
private int[] arr;
//front指针:队头元素的指针
private int front = 0;
//rear指针:队尾元素的的指针
private int rear = 0;
//初始化队列
public ArrayQueue(int size) {
this.arr = new int[size];
}
//判断空队函数
public boolean isEmpty() {
return front == rear;
}
//判断满队函数
public boolean isFull() {
//单向队列,会出现“假溢出”现象
return rear == arr.length;
}
//入队操作
public void inQueue(int e) {
if (isFull()) {
throw new RuntimeException("此队列已满,无法添加元素");
}
arr[rear++] = e;
}
//出队操作
public int outQueue() {
if (isEmpty()) {
throw new RuntimeException("此队列为空,无元素可出队");
}
return arr[front++];
}
//读取队头元素
public int getFront() {
if (isEmpty()) {
throw new RuntimeException("此队列为空,无元素可出队");
}
return arr[front];
}
//遍历队列元素
public void printQueue(){
if(isEmpty()){
System.out.println("此队列为空");
return;
}
System.out.print("front:<<< ");
for (int i = front; i < rear; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("<<<:rear");
}
}
07_Java实现(环形队列)
package DataStructureReview;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//创建队列对象
CircleArrayQueue queue = new CircleArrayQueue(5);
//判断空队
System.out.println("是否空队:" + queue.isEmpty());
//判断满队
System.out.println("是否满队:" + queue.isFull());
//元素进队
queue.inQueue(5);
queue.inQueue(6);
queue.inQueue(7);
queue.inQueue(8);
//queue.inQueue(9);
System.out.println("front元素为:" + queue.getFront());
queue.printQueue();
System.out.printf("%d 出队\n", queue.outQueue());
//"假溢出"现象
queue.inQueue(90);
queue.printQueue();
}
}
class CircleArrayQueue {
private int[] arr;
private int front = 0;
private int rear = 0;
//初始化队列
public CircleArrayQueue(int size) {
this.arr = new int[size];
}
//空队判断
public boolean isEmpty() {
return front == rear;
}
//满队判断
public boolean isFull() {
return (rear + 1) % arr.length == front;
}
//入队操作
public void inQueue(int e) {
if (isFull()) {
throw new RuntimeException("此队列已满,无法进行入队操作");
}
arr[rear] = e;
rear = (rear + 1) % arr.length;
}
//出队操作
public int outQueue() {
if (isEmpty()) {
throw new RuntimeException("此队列为空,无元素出队");
}
int result = arr[front];
front = (front + 1) % arr.length;
return result;
}
//获取front元素
public int getFront() {
if (isEmpty()) {
throw new RuntimeException("此队列为空,无元素出队");
}
int result = arr[front];
return result;
}
//打印队列
public void printQueue() {
if (isEmpty()) {
System.out.println("此队列为空");
return;
}
int front_p = front;
System.out.print("front:<<< ");
while (true) {
System.out.print(arr[front_p] + " ");
front_p = (front_p + 1) % arr.length;
if (front_p == rear) {
break;
}
}
System.out.println("<<<:rear");
}
}