思路分析:
1. front 變量的含義做一個調(diào)整: front 就指向隊列的第一個元素, 也就是說 arr[front] 就是隊列的第一個元素
front 的初始值 = 0
2. rear 變量的含義做一個調(diào)整:rear 指向隊列的最后一個元素的后一個位置. 因為希望空出一個空間做為約定
rear 的初始值 = 0
3. 當隊列滿時,條件是 (rear + 1) % maxSize == front 【滿】
4.隊列為空的條件, rear == front 空
5. 當我們這樣分析, 隊列中有效的數(shù)據(jù)的個數(shù) (rear + maxSize – front) % maxSize // rear = 1 front = 0
6. 我們就可以在原來的隊列上修改得到,一個環(huán)形隊列
java相關(guān)視頻教程分享:java學習視頻
代碼實現(xiàn):
import java.util.Scanner; public class CircleArrayQueueDemo { public static void main(String[] args) { //測試一把 System.out.println("測試數(shù)組模擬環(huán)形隊列的案例~~~"); // 創(chuàng)建一個環(huán)形隊列 CircleArray queue = new CircleArray(4); //說明設(shè)置4, 其隊列的有效數(shù)據(jù)最大是3 char key = ' '; // 接收用戶輸入 Scanner scanner = new Scanner(System.in);// boolean loop = true; // 輸出一個菜單 while (loop) { System.out.println("s(show): 顯示隊列"); System.out.println("e(exit): 退出程序"); System.out.println("a(add): 添加數(shù)據(jù)到隊列"); System.out.println("g(get): 從隊列取出數(shù)據(jù)"); System.out.println("h(head): 查看隊列頭的數(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ù)是%dn", res); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } break; case 'h': // 查看隊列頭的數(shù)據(jù) try { int res = queue.headQueue(); System.out.printf("隊列頭的數(shù)據(jù)是%dn", 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 就指向隊列的第一個元素, 也就是說 arr[front] 就是隊列的第一個元素 //front 的初始值 = 0 private int front; //rear 變量的含義做一個調(diào)整:rear 指向隊列的最后一個元素的后一個位置. 因為希望空出一個空間做為約定. //rear 的初始值 = 0 private int rear; // 隊列尾 private int[] arr; // 該數(shù)據(jù)用于存放數(shù)據(jù), 模擬隊列 public CircleArray(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } // 判斷隊列是否滿 public boolean isFull() { return (rear + 1) % maxSize == front; } // 判斷隊列是否為空 public boolean isEmpty() { return rear == front; } // 添加數(shù)據(jù)到隊列 public void addQueue(int n) { // 判斷隊列是否滿 if (isFull()) { System.out.println("隊列滿,不能加入數(shù)據(jù)~"); return; } //直接將數(shù)據(jù)加入 arr[rear] = n; //將 rear 后移, 這里必須考慮取模 rear = (rear + 1) % maxSize; } // 獲取隊列的數(shù)據(jù), 出隊列 public int getQueue() { // 判斷隊列是否空 if (isEmpty()) { // 通過拋出異常 throw new RuntimeException("隊列空,不能取數(shù)據(jù)"); } // 這里需要分析出 front是指向隊列的第一個元素 // 1. 先把 front 對應的值保留到一個臨時變量 // 2. 將 front 后移, 考慮取模 // 3. 將臨時保存的變量返回 int value = arr[front]; front = (front + 1) % maxSize; return value; } // 顯示隊列的所有數(shù)據(jù) public void showQueue() { // 遍歷 if (isEmpty()) { System.out.println("隊列空的,沒有數(shù)據(jù)~~"); return; } // 思路:從front開始遍歷,遍歷多少個元素 for (int i = front; i < front + size() ; i++) { System.out.printf("arr[%d]=%dn", i % maxSize, arr[i % maxSize]); } } // 求出當前隊列有效數(shù)據(jù)的個數(shù) public int size() { // rear = 2 // front = 1 // maxSize = 3 return (rear + maxSize - front) % maxSize; } // 顯示隊列的頭數(shù)據(jù), 注意不是取出數(shù)據(jù) public int headQueue() { // 判斷 if (isEmpty()) { throw new RuntimeException("隊列空的,沒有數(shù)據(jù)~~"); } return arr[front]; } }
相關(guān)文章教程分享:java快速入門