概述
详情请看代码注释:
package com.example;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Author Gao Xiaozhuang
* @Date 2020/7/27 18:40
* @WARN 要写注释
* @Version 1.0
* @Description
*/
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' ';
Scanner sc = 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):查看队列头数据");
key = sc.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入:");
int value = sc.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
System.out.println("取出的数据是:" + arrayQueue.getQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
System.out.println("取出的数据是:" + arrayQueue.headQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
/**
* 使用数组模拟队列
*/
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;
rear = -1;
}
public boolean isFull() {
return rear == maxSize - 1;
}
public boolean isEmpty() {
return rear == front;
}
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("队列为空,不可遍历");
}
System.out.println(Arrays.toString(arr));
}
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,没有队头");
}
return arr[front + 1];
}
}
环形队列:
package com.example;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Author Gao Xiaozhuang
* @Date 2020/7/27 19:27
* @WARN 要写注释
* @Version 1.0
* @Description环形队列 思路:
* 1.front变量的含义做一个调整:front就指向队列的第一个元素,也就是arr[front]就是队列的第一个元素
* front初始值为 0 rear初始值为 0
* 2.rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定
* 3.当队列满时,条件是(rear+1)%maxSize=front
* 4.当队列空时,rear=front
* 5.队列中有效的数据的个数为 (rear+maxSize-front)%maxSize
*/
public class CircleQueueDemo {
public static void main(String[] args) {
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char key = ' ';
Scanner sc = 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):查看队列头数据");
key = sc.next().charAt(0);
switch (key) {
case 's':
circleArrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入:");
int value = sc.nextInt();
circleArrayQueue.addQueue(value);
break;
case 'g':
try {
System.out.println("取出的数据是:" + circleArrayQueue.getQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
System.out.println("取出的数据是:" + circleArrayQueue.headQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
class CircleArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
/**
* 创建队列构造器
*/
public CircleArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
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("队列为空,不能取数据");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
public void showQueue() {
if (isEmpty()) {
System.out.println("队列为空,不可遍历");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%dn", i % maxSize, arr[i % maxSize]);
}
}
/**
* 求出当前队列有效数据的个数
*
* @return
*/
public int size() {
return (rear + maxSize - front) % maxSize;
}
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,没有队头");
}
return arr[front];
}
}
最后
以上就是诚心盼望为你收集整理的使用数组模拟队列以及优化环形队列详情请看代码注释:环形队列:的全部内容,希望文章能够帮你解决使用数组模拟队列以及优化环形队列详情请看代码注释:环形队列:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复