思路

代碼
package com.atguigu.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//測試一把
System.out.println("測試數(shù)組模擬環(huán)形隊(duì)列的案例~~~");
// 創(chuàng)建一個環(huán)形隊(duì)列
CircleArray queue = new CircleArray(4); //說明設(shè)置4, 其隊(duì)列的有效數(shù)據(jù)最大是3
char key = ' '; // 接收用戶輸入
Scanner scanner = new Scanner(System.in);//
boolean loop = true;
// 輸出一個菜單
while (loop) {
System.out.println("s(show): 顯示隊(duì)列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加數(shù)據(jù)到隊(duì)列");
System.out.println("g(get): 從隊(duì)列取出數(shù)據(jù)");
System.out.println("h(head): 查看隊(duì)列頭的數(shù)據(jù)");
key = scanner.next().charAt(0);// 接收一個字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("輸出一個數(shù)");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g': // 取出數(shù)據(jù)
try {
int res = queue.getQueue();
System.out.printf("取出的數(shù)據(jù)是%d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case 'h': // 查看隊(duì)列頭的數(shù)據(jù)
try {
int res = queue.headQueue();
System.out.printf("隊(duì)列頭的數(shù)據(jù)是%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 CircleArray {
private int maxSize; // 表示數(shù)組的最大容量
//front 變量的含義做一個調(diào)整: front 就指向隊(duì)列的第一個元素, 也就是說 arr[front] 就是隊(duì)列的第一個元素
//front 的初始值 = 0
private int front;
//rear 變量的含義做一個調(diào)整:rear 指向隊(duì)列的最后一個元素的后一個位置. 因?yàn)橄M粘鲆粋€空間做為約定.
//rear 的初始值 = 0
private int rear; // 隊(duì)列尾
private int[] arr; // 該數(shù)據(jù)用于存放數(shù)據(jù), 模擬隊(duì)列
public CircleArray(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
}
// 判斷隊(duì)列是否滿
public boolean isFull() {
return (rear? + 1) % maxSize == front;
}
// 判斷隊(duì)列是否為空
public boolean isEmpty() {
return rear == front;
}
// 添加數(shù)據(jù)到隊(duì)列
public void addQueue(int n) {
// 判斷隊(duì)列是否滿
if (isFull()) {
System.out.println("隊(duì)列滿,不能加入數(shù)據(jù)~");
return;
}
//直接將數(shù)據(jù)加入
arr[rear] = n;
//將 rear 后移, 這里必須考慮取模
rear = (rear + 1) % maxSize;
}
// 獲取隊(duì)列的數(shù)據(jù), 出隊(duì)列
public int getQueue() {
// 判斷隊(duì)列是否空
if (isEmpty()) {
// 通過拋出異常
throw new RuntimeException("隊(duì)列空,不能取數(shù)據(jù)");
}
// 這里需要分析出 front是指向隊(duì)列的第一個元素
// 1. 先把 front 對應(yīng)的值保留到一個臨時變量
// 2. 將 front 后移, 考慮取模
// 3. 將臨時保存的變量返回
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
// 顯示隊(duì)列的所有數(shù)據(jù)
public void showQueue() {
// 遍歷
if (isEmpty()) {
System.out.println("隊(duì)列空的,沒有數(shù)據(jù)~~");
return;
}
// 思路:從front開始遍歷,遍歷多少個元素
// 動腦筋
for (int i = front; i < front + size() ; i++) {
System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
}
}
// 求出當(dāng)前隊(duì)列有效數(shù)據(jù)的個數(shù)
public int size() {
// rear = 2
// front = 1
// maxSize = 3
return (rear + maxSize - front) % maxSize;?
}
// 顯示隊(duì)列的頭數(shù)據(jù), 注意不是取出數(shù)據(jù)
public int headQueue() {
// 判斷
if (isEmpty()) {
throw new RuntimeException("隊(duì)列空的,沒有數(shù)據(jù)~~");
}
return arr[front];
}
}