队列
队列是一个有序列表,可以用数组和链表实现\
数组实现队列
package dataStructure.Queue;
import java.util.Scanner;
/**
* @author lhn
* @date 2024-04-10 11:52
*/
public 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[maxSize];
front = -1; //指向队列头部,指向队列头的前一个位置
rear = -1; //指向队列尾部,指向队列尾的数据
}
public boolean isFull() {
return rear == this.maxSize - 1;
}
public boolean isEmpty() {
return this.front == this.rear;
}
public void addQueue(int n) {
if (isFull()){
System.out.println("队列已满不能加入数据");
return;
}
rear++;
this.arr[rear] = n;
}
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能取数据");
}
front++;
return this.arr[front];
}
public void showQueue() {
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = this.front + 1; i < this.front + this.maxSize; i++) {
System.out.print(this.arr[i] + " ");
}
System.out.println(); // 换行
}
public int headQueue() {
if(isEmpty()){
throw new RuntimeException("队列为空");
} else {
return this.arr[++front];
}
}
public static void main(String[] args){
//创建一个队列
ArrayQueue arrayQueue = new ArrayQueue(3);
char Key = ' ';//接口用户输入
Scanner s= 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 = s.next().charAt(0);//接收一个字符
switch (Key){
case 's':
arrayQueue.showQueue();
System.out.println("show funish");
break;
case 'a':
System.out.println("请输入一个数");
int value= s.nextInt();
// System.out.println("value is " + value);
arrayQueue.addQueue(value);
break;
case 'g':
try{
int res = arrayQueue.getQueue();
System.out.println("去除的数据为:"+res);
}catch (Exception e){
System.out.println(e.getMessage());//捕捉异常信息
}
break;
case 'h':
try{
int res = arrayQueue.headQueue();
System.out.println("去除的数据为:"+res);
}catch (Exception e){
System.out.println(e.getMessage());//捕捉异常信息
}
break;
case 'e':
s.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
使用数组模拟环形队列
环形队列代码实现
package dataStructure.Queue;
import java.util.Scanner;
/**
* @author lhn
* @date 2024-04-12 10:32
*/
public class CircleArrayQueue {
private int maxSize; // 数组最大容量
private int front = 0; // 队列头
private int rear = 0; // 指向队列尾部,指向队列尾的后一个位置
private int[] arr; // 该数据的存入位置
public CircleArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
}
public boolean isFull() {
return (this.rear + 1) % this.maxSize == this.front;
}
public boolean isEmpty() {
return this.front == this.rear;
}
public void addQueue(int n) {
if (isFull()){
System.out.println("队列已满不能加入数据");
return;
}
this.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 int size() {
// System.out.println("size() is used size is " + (rear + maxSize - front) % maxSize);
return (rear + maxSize - front) % maxSize;
}
public void showQueue() {
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = this.front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d\n",i % maxSize, arr[i % maxSize]);
}
}
public int headQueue() {
if(isEmpty()){
throw new RuntimeException("队列为空");
} else {
return this.arr[front];
}
}
public static void main(String[] args) {
//创建一个队列
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char Key = ' ';//接口用户输入
Scanner s= 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 = s.next().charAt(0);//接收一个字符
switch (Key){
case 's':
circleArrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数");
int value= s.nextInt();
// System.out.println("value is " + value);
circleArrayQueue.addQueue(value);
break;
case 'g':
try{
int res = circleArrayQueue.getQueue();
System.out.println("去除的数据为:"+res);
}catch (Exception e){
System.out.println(e.getMessage());//捕捉异常信息
}
break;
case 'h':
try{
int res = circleArrayQueue.headQueue();
System.out.println("头部队列的数据为:"+res);
}catch (Exception e){
System.out.println(e.getMessage());//捕捉异常信息
}
break;
case 'e':
s.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出");
}
}