1.数组队列
2020-10-05 本文已影响0人
据分专家
数组实现单队列
package com.zhiyang.queuearr;
import java.util.Scanner;
import javax.swing.text.AbstractDocument.BranchElement;
public class arrqueue {
public static void main(String[] args) {
//创建一个对象
ArrayQueue arrayQueue=new ArrayQueue(3);
char key=' ';//接收用户输入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while(loop) {
System.out.println("s:展示队列数据");
System.out.println("e:退出");
System.out.println("a:添加数据");
System.out.println("g:取数据");
System.out.println("h:查看队头数据");
key=scanner.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数字\n");
int value=scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res=arrayQueue.getQueue();
System.out.printf("出队的为:%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res=arrayQueue.headQueue();
System.out.printf("队列头为:%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
//使用数组模拟队列-编写一个arr-queue的队列
class ArrayQueue {
private int maxSize;
private int front;// queue的头
private int rear;// queue的尾
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("Queue is full");
return;
}
rear++;
arr[rear]=n;
}
//元素出队
public int getQueue() {
if(isEmpty()) {
throw new RuntimeException("Queue is empty");
}
front++;
System.out.printf("运行此处");
return arr[front];
}
//显示队列的所有数据
public void showQueue() {
if(isEmpty()) {
System.out.println("Queue is empty");
}
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("Queue is empty");
}
return arr[front+1];
}
}
数组实现循环队列
import java.util.Scanner;
public class CircleArrayQueue {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个对象
CircleArrQueue arrayQueue=new CircleArrQueue(4);//最大元素为3
char key=' ';//接收用户输入
Scanner scanner=new Scanner(System.in);
boolean loop=true;
while(loop) {
System.out.println("s:展示队列数据");
System.out.println("e:退出");
System.out.println("a:添加数据");
System.out.println("g:取数据");
System.out.println("h:查看队头数据");
key=scanner.next().charAt(0);
switch (key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数字\n");
int value=scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res=arrayQueue.getQueue();
System.out.printf("出队的为:%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res=arrayQueue.headQueue();
System.out.printf("队列头为:%d\n",res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
class CircleArrQueue {
private int maxSize;
private int front;// queue的头,指向队列第一个元素
private int rear;// queue的尾,指向队列最后元素的下一个元素
private int[] arr;
public CircleArrQueue(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("Queue is full");
return;
}
// 直接将数据加入
arr[rear] = n;
// rear后移取模
rear = (rear + 1) % maxSize;
}
// 元素出队
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("Queue is empty");
}
// front指向队列第一个元素
// 1.先将取出的数据保存
// 2.然后重新设置front的值,此处之所以不考虑直接使用front-1来取值,
// 是考虑到front可能会取模,此时会产生数组越界的错误
// 3.将取出的值返回
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
// 显示队列的所有数据
public void showQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
for (int i = front; i <front+size(); i++) {
//当所取元素在front后时,i是小于maxsize的,此时数组不越界
//但当元素在front前时,需要取模来避免数组越界
System.out.printf("arr[%d]=%d\n", i, arr[i%maxSize]);
}
}
//显示当前队列有效数据的个数
public int size() {
return (rear-front+maxSize)%maxSize;
}
//显示头结点
public int headQueue() {
if(isEmpty()) {
throw new RuntimeException("Queue is empty");
}
return arr[front];
}
}