一次性队列的实现
package com.hand.deque;
/**
*
* 基于数组实现的一次性队列
*/
public class ArrayQueue {
/**
* 数组的最大容量
*/
private int maxSize;
/**
* 队列头
*/
private int front;
/**
* 队列尾
*/
private int rear;
/**
* 队列的数据
*/
private int[] data;
/**
* 构造器
*
* @param arrayMaxSize 数组最大值
*/
public ArrayQueue(int arrayMaxSize) {
maxSize = arrayMaxSize;
data = new int[arrayMaxSize];
front = -1;
rear = -1;
}
/**
* 队列是否已满
*
* @return
*/
public boolean isFull() {
return rear == maxSize - 1;
}
/**
* 队列是否为空
*
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 向队列中添加元素
*
* @param n
*/
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已经满.");
throw new RuntimeException("队列已经满.");
}
rear++;
data[rear] = n;
}
/**
* 取出队列的头元素
*
* @return
*/
public int getQueue() {
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
front++;
return data[front];
}
/**
* 查看队列中的全部的数据
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
for (int i = 0; i < data.length; i++) {
System.out.printf("array[%d]=%d\n", i, data[i]);
}
}
/**
* 查看队列的第一个元素
*
* @return
*/
public int headQueue() {
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
return data[front + 1];
}
}
环形队列的实现
package com.hand.deque;
/**
* 基于数组的环形队列
*
*
* 约定如下:
* front 指向队列的第一个元素 array[front]为队列的第一个元素 front的初始值为0
* rear 指向队列的最后一个元素的后一个位置 rear的初始值为0
*
*
* 队列满的条件: ( rear + 1 ) % maxSize == front
* 队列空的条件: front == rear
* 队列中有效数据个数: ( rear + maxSize - front ) % maxSize
*
* 尾索引的下一个为头索引时表示队列满 即将队列容量空出一个作为约定。
*/
public class CircleQueue {
/**
* 最大容量
*/
private int maxSize;
/**
* 队列尾 指向最后一个元素的后一个位置
*/
private int rear;
/**
* 指向队列头
*/
private int front;
/**
* 用于存放数据
*/
private int[] data;
/**
* 构造函数初始化队列
*
* @param arrayMaxSize
*/
public CircleQueue(int arrayMaxSize) {
maxSize = arrayMaxSize;
data = new int[arrayMaxSize];
front = 0;
rear = 0;
}
/**
* 判断队列是否已满
*
* @return
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 队列是否为空
*
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 向队列中添加元素
*
* @param n
*/
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已经满.");
throw new RuntimeException("队列已经满.");
}
//直接将数据加入
data[rear] = n;
//将数据后移 后移动时不可越界 需要加取模
rear = (rear + 1) % maxSize;
}
/**
* 取出队列的头元素
*
* @return
*/
public int getQueue() {
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
//保存值到一个临时变量
int value = data[front];
//front后移 后移动时不可越界 需要加取模
front = (front + 1) % maxSize;
return value;
}
/**
* 查看队列中的全部的数据
*/
public void showQueue() {
//有效数据的个数
int size = (rear + maxSize - front) % maxSize;
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
//从front开始遍历 遍历指定个数的元素
for (int i = front; i < front + size; i++) {
System.out.printf("array[%d]=%d\n", i % maxSize, data[i % maxSize]);
}
}
/**
* 查看队列的第一个元素
*
* @return
*/
public int headQueue() {
if (isEmpty()) {
System.out.println("队列为空.");
throw new RuntimeException("队列为空.");
}
//直接返回
return data[front];
}
}