Java数据结构与算法 |数组模拟环形队列


一、队列介绍

队列是一个有序列表,可以用数组或者链表来实现。

遵循先入先出的原则。即先存入队列的数据要先取出,后存入队列的数据要后取出。

  • 队列示意图(使用数组模拟)
    在这里插入图片描述

  • 应用场景:银行挂号系统。
    当有一个服务台的业务员为一个客户服务完毕后,就会依次叫号。
    在这里插入图片描述

二、数组模拟队列

数组模拟队列思路分析

设计队列时,maxSize为队列最大容量;由于队列是数据后端进前端出,所以使用两个变量frontrear分别标记队列的前端和后端;假设将数据存入队列称为addQueue,当队列不满时才可以向队列中添加数据;当队列不空时才可以从队列取值。①队空:front==rear②队满:rear==maxSize-1

front指向的是队列头的前一个位置,即队列第一个元素为arr[front+1]
rear执行队列的最后一个元素,即队列最后一个元素为arr[rear]

代码实现

package com.gql.queue;

import java.util.Scanner;

/**
 * 数组模拟队列 Version1
 * 
 * @guoqianliang
 */
public class ArrayQueueDemo {
	public static void main(String[] args) {
		// 1.创建队列
		ArrayQueue queue = new ArrayQueue(3);
		char key = ' ';
		Scanner in = new Scanner(System.in);
		boolean flag = true;
		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("-----------------------");
		while (flag) {
			key = in.next().charAt(0);
			switch (key) {
			case 's':// 打印队列
				queue.showQueue();
				break;
			case 'a':// 添加数据
				System.out.println("请输入要添加的数据:");
				int value = in.nextInt();
				queue.addQueue(value);
				break;
			case 'g':// 取出数据
				try {
					int result = queue.getQueue();
					System.out.printf("取出的数据是%d\n", result);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h':// 取队列头数据
				try {
					int result = queue.headQueue();
					System.out.printf("队列头的数据是%d\n", result);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'e':// 退出
				in.close();
				flag = false;
				break;
			default:
				break;
			}
		}
		System.out.println("程序已退出~");
	}
}

//使用数组模拟队列
class ArrayQueue {
	private int maxSize;// 数组的最大容量
	private int front;// 队头
	private int rear;// 队尾
	private int[] arr;// arr数组模拟队列

	// 创建队列
	public ArrayQueue(int arrmaxSize) {
		maxSize = arrmaxSize;
		arr = new int[maxSize];
		front = -1;// 指向队列头的前一个位置
		rear = -1;// 指向队列尾的位置
	}

	// 判断队满
	public boolean isFull() {
		return rear == maxSize - 1;
	}

	// 判断队空
	public boolean isEmpty() {
		return rear == front;
	}

	// 后端入队列--->队列不能满
	public void addQueue(int n) {// 参数n代表要添加的数据
		if (isFull()) {
			System.out.println("队列满,不能存入数据~");
		} else {
			rear++;// 尾指针后移
			arr[rear] = n;// 将要添加的数据放入队尾
		}
	}

	// 前端出队列--->队列不能空
	public int getQueue() {
		if (isEmpty()) {
			throw new RuntimeException("队列空,不能获得数据~");
		}
		front++;// 头指针后移
		return arr[front];
	}

	// 打印队列的所有数据
	public void showQueue() {
		if (isEmpty()) {
			System.out.println("队列为空,没有数据~");
		} else {
			for (int i = 0; i < arr.length; i++) {
				System.out.printf("arr[%d]=%d\n", i, arr[i]);
			}
		}
	}

	// 显示队头数据,仅显示
	public int headQueue() {
		if (isEmpty()) {
			throw new RuntimeException("队列空,不能显示队头数据~");
		}
		return arr[front + 1];
	}
}

在这里插入图片描述
上面的代码的缺点是只能使用一次,无法复用,如果希望多次使用应该使用下面的环形队列。

三、数组模拟环形队列

数组模拟环形队列思路分析

front的含义做调整:使front指向队列第一个元素,即队列第一个元素为arr[front]。front的初始值置为0。

rear的含义做调整:使rear指向队列的最后一个元素的后一个位置,即队列最后一个元素为arr[rear-1]。rear的初始值置为0。

扫描二维码关注公众号,回复: 11208442 查看本文章

rear+1是为了预留一个空间来做约定,区分队列满和队列空两种状态,如果不空出来,判断满和判断空时的条件就一样了。

队空的条件rear == front

队满的条件改变了:之前不使用环形队列时,队满的条件是:rear ==maxSize-1;现在使用了环形队列,队满的条件变成了:(rear + 1 - front)%maxSize == 0

队列中有效的数据个数为:(rear+maxSize-front)%maxSize,如图即:(6+7-0)%7==6

在这里插入图片描述

代码实现

package com.gql.queue;

import java.util.Scanner;

/**
 * 数组模拟队列 Version2
 * 
 * @guoqianliang
 */
public class CircularQueueDemo {
	public static void main(String[] args) {
		// 1.创建队列
		CircularQueue queue = new CircularQueue(4);// 队列有效数据为3
		char key = ' ';
		Scanner in = new Scanner(System.in);
		boolean flag = true;
		System.out.println("测试数组模拟环形队列的案例~~");
		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("-----------------------");
		while (flag) {
			key = in.next().charAt(0);
			switch (key) {
			case 's':// 打印队列
				queue.showQueue();
				break;
			case 'a':// 添加数据
				System.out.println("请输入要添加的数据:");
				int value = in.nextInt();
				queue.addQueue(value);
				break;
			case 'g':// 取出数据
				try {
					int result = queue.getQueue();
					System.out.printf("取出的数据是%d\n", result);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h':// 取队列头数据
				try {
					int result = queue.headQueue();
					System.out.printf("队列头的数据是%d\n", result);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'e':// 退出
				in.close();
				flag = false;
				break;
			default:
				break;
			}
		}
		System.out.println("程序已退出~");
	}
}

//使用数组模拟队列
class CircularQueue {
	private int maxSize;// 数组的最大容量
	private int front;// 队头
	private int rear;// 队尾
	private int[] arr;// arr数组模拟队列

	// 创建队列
	public CircularQueue(int arrmaxSize) {
		maxSize = arrmaxSize;
		arr = new int[maxSize];
		front = 0;//
		rear = 0;// front和rear的初始值都置为0
	}

	// 判断队满
	public boolean isFull() {
		return (rear + 1 - front) % maxSize == 0;
	}

	// 判断队空
	public boolean isEmpty() {
		return rear == front;
	}

	// 后端入队列--->队列不能满
	public void addQueue(int n) {// 参数n代表要添加的数据
		if (isFull()) {
			System.out.println("队列满,不能存入数据~");
		} else {
			arr[rear] = n;// 直接将要添加的数据放入队尾
			rear = (rear + 1) % maxSize;// 尾指针后移
		}
	}

	// 前端出队列--->队列不能空
	public int getQueue() {
		if (isEmpty()) {
			throw new RuntimeException("队列空,不能获得数据~");
		}
		// 1.先把front对应的值保存到一个临时变量
		int value = arr[front];
		// 2.再将front后移,考虑取模
		front = (front + 1) % maxSize;
		// 3.将临时保存的变量返回
		return value;
	}

	// 打印队列的所有数据
	public void showQueue() {
		if (isEmpty()) {
			System.out.println("队列为空,没有数据~");
		} else {
			// 从front开始遍历,
			for (int i = front; i < front + size(); i++) {
				System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
			}
		}
	}

	// 返回当前队列有效的数据个数
	public int size() {
		return (rear + maxSize - front) % maxSize;
	}

	// 显示队头数据,仅显示
	public int headQueue() {
		if (isEmpty()) {
			throw new RuntimeException("队列空,不能显示队头数据~");
		}
		return arr[front];
	}
}

在这里插入图片描述

原创文章 475 获赞 1731 访问量 58万+

猜你喜欢

转载自blog.csdn.net/weixin_43691058/article/details/105607986