队列
基本介绍
- 队列是一个有序列表,可以用数组或是链表来实现。
- 遵循先入先出原则。
- 示意图:(使用数组模拟队列示意图)
数组模拟队列、
思路
- 如下图:front指向队列头部即指向队列头的前一个位置,rear指向队尾即指向队列尾的数据,maxSize是队列的最大容量。front随着数据输出而改变,rear随着数据输入而改变。
- front和rear的初始值为-1。
- 空条件:
front == rear
- 满条件:
rear == maxSize - 1
代码
package com.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
System.out.println("~~~~~~~~~~~~~~~~~~~");
System.out.println("a -> 添加");
System.out.println("g -> 获取");
System.out.println("s -> 显示队列");
System.out.println("h -> 显示队头");
System.out.println("e -> 退出");
System.out.println("~~~~~~~~~~~~~~~~~~~");
ArrayQueue arrayQueue = new ArrayQueue(3);
char c = ' ';
boolean loop = true;
Scanner scanner = new Scanner(System.in);
while(loop){
System.out.println("请输入操作选项:");
c = scanner.next().charAt(0);
switch (c){
case 'a':
System.out.println("请输入添加的数据:");
int add = scanner.nextInt();
arrayQueue.addQueue(add);
break;
case 'g':
try {
arrayQueue.getQueue();
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 's':
arrayQueue.showQueue();
break;
case 'h':
try {
arrayQueue.showHead();
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
}
}
class ArrayQueue{
private int maxSize;//数组最大值
private int front;//队首
private int rear;//队尾
private int[] arr;//存放数据的数组
public ArrayQueue(int maxSize){
this.maxSize = maxSize;
arr = new int[maxSize];
this.front = -1;
this.rear = -1;
}
/**
* 判断队列是否满
* @return
*/
public boolean isFull(){
return this.rear == maxSize - 1;
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty(){
return this.front == this.rear;
}
/**
* 向队列中插入元素
* @param n
*/
public void addQueue(int n){
if(isFull()){
System.out.println("队列已经满,不能插入~~");
return;
}
arr[++this.rear] = n;
}
/**
* 获取队列元素
* @return
*/
public int getQueue(){
if(isEmpty()){
throw new RuntimeException("队列为空~~");
}
return arr[++this.front];
}
/**
* 显示队列
*/
public void showQueue(){
if(isEmpty()){
System.out.println("队列为空!");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
/**
* 显示队首
*/
public void showHead(){
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
System.out.println("队首为:"+arr[this.front+1]);
}
}
缺点
数组只能使用一次。无法达到复用效果。
数组模拟环形队列
思路
- 将数组看作环形,通过取模的方式实现。
- front指向队列的第一个元素。
- rear指向队列的最后一个元素的后一个位置。
- front 和 rear 初始值都为 0。
- 有效数据个数:
(rear - front + maxSize) % maxSize
- 空条件:
(rear + 1) % maxSize == front
- 满条件:
rear == front
代码
package com.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
System.out.println("~~~~~~~~~~~~~~~~~~~");
System.out.println("a -> 添加");
System.out.println("g -> 获取");
System.out.println("s -> 显示队列");
System.out.println("h -> 显示队头");
System.out.println("e -> 退出");
System.out.println("~~~~~~~~~~~~~~~~~~~");
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char c = ' ';
boolean loop = true;
Scanner scanner = new Scanner(System.in);
while(loop){
System.out.println("请输入操作选项:");
c = scanner.next().charAt(0);
switch (c){
case 'a':
System.out.println("请输入添加的数据:");
int add = scanner.nextInt();
circleArrayQueue.addQueue(add);
break;
case 'g':
try {
int queue = circleArrayQueue.getQueue();
System.out.println("取走:"+queue);
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 's':
circleArrayQueue.showQueue();
break;
case 'h':
try {
circleArrayQueue.showHead();
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
}
}
class CircleArrayQueue{
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArrayQueue(int maxSize){
this.maxSize = maxSize;
arr = new int[maxSize];
}
public boolean isFull(){
return (this.rear + 1) % maxSize == this.front;
}
public boolean isEmpty(){
return this.front == this.rear;
}
public void addQueue(int n){
if(isFull()){
System.out.println("队列已经满,不能插入~~");
return;
}
arr[this.rear] = n;
this.rear = (this.rear + 1) % maxSize;
}
public int getQueue(){
if(isEmpty()){
throw new RuntimeException("队列为空~~");
}
int value = arr[this.front];
this.front = (this.front + 1) % maxSize;
return value;
}
public void showQueue(){
if(isEmpty()){
System.out.println("队列为空!");
return;
}
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 void showHead(){
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
System.out.println("队首为:"+arr[this.front]);
}
}