數(shù)組模擬環(huán)形隊(duì)列

思路

代碼


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];

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容