队列是一种先进先出的有序列表,可以用数组或者链表来实现(例如:银行排队系统)
前提条件:
maxSize :队列容量(数组的长度)
arr :模拟队列的数组
front :指向队列头部元素的前一个元素,初始值为 -1
rear :指向队列尾部元素,初始值为 -1
首先判断:
队列判空:front == rear
队列判满:rear == (maxSize - 1) ,即 rear 是否已经指向了数组的最后一个位置
队列元素个数:rear - front
队列入队:队列不满才能入队,arr[++rear] = value
队列出队:队列不空才能出队,return arr[front++]
首先我们来实现一下一次性的数组模拟队列,即我们在这个数组中添加了数据直到到达了rear=maxSize-1,这时我们再取出数据,尽管数组前面还有空间,但是rear始终等于maxSize-1,我们仍无法向其中添加数据,这就是假溢出,造成了资源的浪费,具体代码如下:
import java.util.Scanner;
public class Queue {
public static void main(String[] args) {
ArrayQueue queue=new ArrayQueue(3);
char key=' ';//接收用户输入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while (loop){
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
System.out.println();
key=scanner.next().charAt(0);//接收一个字符
switch (key){
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value=scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res=queue.getQueue();
System.out.printf("去除的数据是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res=queue.headQueue();
System.out.printf("队列的头部是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
default:
break;
}
}
System.out.println("程序退出");
}
static class ArrayQueue{
private int maxSize;//表示数组的最大容量
private int front;//队列头部,是首节点的前一个位置
private int rear;//队列尾部
private int[] arr;//模拟队列
public ArrayQueue(int arrMaxSize) {
maxSize=arrMaxSize;
arr=new int[maxSize];
front=-1;//指向队列头部,分析出front是指向队列头的前一个位置
rear=-1;//指向队列尾部.指向队列的最后一个数据
}
//判断队列是否为满
public boolean isFull(){
return rear==maxSize-1;
}
//判断队列是否为空
public boolean isEmpty(){
return front==rear;
}
//添加数据到队列
public void addQueue(int n){
if (isFull()){
System.out.println("队列已经满了,不能添加数据");
return;
}
rear++;
arr[rear]=n;
}
//获取队列的数据,出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,不能去除数据");
}
front++;
return arr[front];
}
//显示队列的所有数据
public void showQueue(){
//遍历
if (isEmpty()){
System.out.println("队列是空的,没有数据");
return;
}
for (int i = front+1; i <=rear ; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
System.out.println();
}
// 显示队列的头数据.注意不是取出数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列空的,没有数据");
}
return arr[front+1];
}
}
}
然后我们便可以考虑"升级"一下这个版本,即我们可以使用数组来模拟循环队列,这样我们便可以更大程度来使用队列,对前面的队列进行优化,改造为环形队列(通过取模实现)
前提条件
maxSize :队列容量(数组的长度)
arr :模拟队列的数组
front :指向队列头部元素,初始值为 0
rear :指向队列尾部元素的后一个元素,初始值为 0
首先判断:
代码如下:
import java.util.Scanner;
public class CircleQueueArray {
public static void main(String[] args) {
Circle queue=new Circle(4);
char key=' ';//接收用户输入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while (loop){
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
System.out.println();
key=scanner.next().charAt(0);//接收一个字符
switch (key){
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value=scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res=queue.getQueue();
System.out.printf("去除的数据是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res=queue.headQueue();
System.out.printf("队列的头部是%d\n",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
default:
break;
}
}
System.out.println("程序退出");
}
static class Circle {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public Circle(int arrMaxSize) {
maxSize = arrMaxSize;
front = 0;
rear = 0;
arr = new int[maxSize];
}
//判断队列是否为满
public boolean isFull() {
return (rear+1)%maxSize==front;
}
//判断队列是否为空
public boolean isEmpty() {
return rear==front;
}
//添加数据到队列
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列已经满了,不能添加数据");
return;
}
arr[rear]=n;
rear=(rear+1)%maxSize;
}
//获取队列的数据,出队列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能去除数据");
}
// 这里需要分析出 front是指向队列的第一个元素
// 1. 先把 front 对应的值保留到一个临时变量
// 2. 将 front 后移, 考虑取模
// 3. 将临时保存的变量返回
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
//显示队列的所有数据
public void showQueue() {
//遍历
if (isEmpty()) {
System.out.println("队列是空的,没有数据");
return;
}
for (int i = front ; i <front+((rear-front+maxSize)%maxSize); i++) {
System.out.printf("arr[%d]=%d\n", (i+maxSize)%maxSize, arr[i%maxSize]);
}
}
// 显示队列的头数据.注意不是取出数据
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列空的,没有数据");
}
return arr[front];
}
}
}