三、數組模擬環形隊列

package text;

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        // 測試數組模擬環形隊列
        System.out.println("測試數組模擬環形隊列");
        // 創建一個環形隊列
        CircleArray queue = new CircleArray(4); // 說明設置4,其隊列的有效數據最大是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):添加數據到隊列");
            System.out.println("g(get):從隊列中取出數據");
            System.out.println("h(head):查看隊列頭的數據");

            key = scanner.next().charAt(0); // 接收一個字符
            switch (key) {
            case 's':
                queue.showQueue();
                break;
            case 'a':
                System.out.println("輸入一個數");
                int value = scanner.nextInt();
                queue.addQueue(value);
                break;
            case 'g':
                try {
                    int res = queue.getQueue();
                    System.out.printf("取出的數據是%d\n", res);
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println(e.getMessage());
                }
                break;
            case 'h':
                try {
                    int res = queue.headQueue();
                    System.out.printf("隊列的頭數據是%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; // 表示數組的最大容量
    // front就指向隊列的第一個元素,也就是說arr[front]就是隊列的第一個元素,front的初始值爲0
    private int front;
    // rear指向隊列的最後一個元素的後一個位置,因爲希望空出一個空間做約定,rear的初始值爲0
    private int rear;
    private int[] arr; // 該數組用於存放數據,模擬隊列

    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    // 判斷隊列是否滿
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    // 判斷隊列是否爲空
    public boolean isEmpty() {
        return rear == front;
    }

    // 添加數據到隊列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("隊列滿,不能加入數據..");
        }
        // 直接將數據加入
        arr[rear] = n;
        // 將rear後移,這裏必須考慮取模
        rear = (rear + 1) % maxSize;
    }

    // 獲取隊列的數據
    public int getQueue() {
        // 判斷隊列是否爲空
        if (isEmpty()) {
            // 通過拋異常
            throw new RuntimeException("隊列空,不能取數據");
        }
        // 這裏需要分析出front是指向隊列的第一個元素
        // 1、先把front對應的值保留到一個臨時變量
        // 2、將front後移,考慮取模
        // 3、將臨時保存的變量返回
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    // 顯示隊列的所有數據
    public void showQueue() {
        // 遍歷
        if (isEmpty()) {
            System.out.println("隊列爲空,沒有數據");
            return;
        }
        // 從front開始,遍歷多個元素
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    // 求出當前隊列的有效數據的個數
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    // 顯示隊列的頭數據,注意不是取數據
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("隊列數據爲空");
        }
        return arr[front];
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章