非循环队列
非循环队列会不断的使得下标上移,造成”一次性“的浪费
队列定义
class ArrayQueue {
private int maxSize; //最大容量
private int front; //队列头 指向头元素的前一个元素
private int rear; //队列尾 指向尾元素
private int[] arr; //数据队列
//创建队列构造器
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[this.maxSize];
front = -1; //指向头部数据的前一个位置
rear = -1; //指向队列尾部,最后一个数组
}
判断队列是否为空,为满
//判断队列是否满
public boolean isFull() {
return rear == this.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("队列为空,没用数据");
return;
}
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];
}
完整代码
package com.atguigu.queue;
import java.util.Scanner;
import javax.management.RuntimeErrorException;
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(3);
char key;
Scanner scaner = 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 = scaner.next().charAt(0);//接收一个字符
switch(key) {
case 's':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scaner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.printf("去除数据是%d\n",res);
}
catch(Exception E){
System.out.println(E.getMessage());
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.printf("队列头数据是%d\n",res);
}
catch(Exception E){
System.out.println(E.getMessage());
}
break;
case 'e':
scaner.close();
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 maxSize) {
this.maxSize = maxSize;
this.arr = new int[this.maxSize];
front = -1; //指向头部数据的前一个位置
rear = -1; //指向队列尾部,最后一个数组
}
//判断队列是否满
public boolean isFull() {
return rear == this.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("队列为空,没用数据");
return;
}
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];
}
}