數(shù)組模擬環(huán)形隊(duì)列
充分利用數(shù)組,因此將數(shù)組看做是一個(gè)環(huán)形的。(通過取模的方式來實(shí)現(xiàn)即可)
思路如下:
1.font變量的含義做一個(gè)調(diào)整:font就指向隊(duì)列的第一個(gè)元素,也就是說arr[font]就是隊(duì)列的第一個(gè)元素,front的初始值是0
2.rear變量的含義做一個(gè)調(diào)整:rear指向隊(duì)列的最后一個(gè)元素的后一個(gè)位置,因?yàn)橄M粘鲆粋€(gè)空間作為約定,rear的初始值=0
3.當(dāng)隊(duì)列滿時(shí),條件是 (rear+1)%maxSize == font 【滿】
4.當(dāng)隊(duì)列為空的條件,rear==front,【空】
5.隊(duì)列中有效的數(shù)據(jù)的個(gè)數(shù):(rear+maxSize-front)%maxSize
代碼實(shí)現(xiàn)
package com.young.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
System.out.println("測試數(shù)組模擬環(huán)形隊(duì)列的案例~~~");
//創(chuàng)建一個(gè)環(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;
//輸出一個(gè)菜單
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);//接收一個(gè)字符
switch (key) {
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("輸出一個(gè)數(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) {
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) {
System.out.println(e.getMessage());
}
break;
case 'e'://退出
scanner.close();
loop = false;
default:
break;
}
}
System.out.println("程序退出~~");
}
}
class CircleArray {
private int maxSize;//表示數(shù)組的最大容量
/**
* font變量的含義做一個(gè)調(diào)整:font就指向隊(duì)列的第一個(gè)元素,也就是說arr[font]就是隊(duì)列的第一個(gè)元素,front的初始值是0
*/
private int front;
/**
* rear變量的含義做一個(gè)調(diào)整:rear指向隊(duì)列的最后一個(gè)元素的后一個(gè)位置,因?yàn)橄M粘鲆粋€(gè)空間作為約定,rear的初始值=0
*/
private int rear;
private int[] arr;//該數(shù)組用于存放數(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ì)列的第一個(gè)元素
//1.先把這個(gè)front對應(yīng)的值保留到一個(gè)臨時(shí)變量
int value = arr[front];
//2.將front后移,考慮取模
front = (front + 1) % maxSize;
//3.將臨時(shí)保存的變量返回
return value;
}
//顯示隊(duì)列的所有數(shù)據(jù)
public void showQueue() {
//遍歷
if (isEmpty()) {
System.out.println("隊(duì)列空的,沒有數(shù)據(jù)");
return;
}
//思路:從front開始遍歷,遍歷多少個(gè)元素
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ù)的個(gè)數(shù)
public int size() {
return (rear + maxSize - front) % maxSize;
}
//顯示隊(duì)列的頭數(shù)據(jù),注意不是取出數(shù)據(jù)
public int headQueue() {
//判斷
if (isEmpty()) {
throw new RuntimeException("隊(duì)列空的,沒有數(shù)據(jù)~~");
}
return arr[front];
}
}