天天看點

數組,隊列及環形隊列【Java實作】1. 數組模拟隊列2. 數組模拟環形隊列3. 測試類

文章目錄

  • 1. 數組模拟隊列
  • 2. 數組模拟環形隊列
  • 3. 測試類

1. 數組模拟隊列

使用數組實作隊列的先進先出等操作

示例代碼:

public class Queue {
    //隊列的最大容量
    private int maxSize;
    //隊列中第一個資料的索引
    private int front;
    //隊列中最後一個資料的索引
    private int rear;
    //模拟隊列的數組
    private int[] arr;

    //構造函數初始化隊列
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        //初始隊列沒有資料
        front = -1;
        rear = -1;
    }

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

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

    //往隊列中添加資料
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("隊列已滿,無法添加資料!");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    //從隊列中擷取資料
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("隊列為空,無法擷取資料!");
        }
        front++;
        return arr[front];
    }

    //擷取隊頭資料
    public int getHead() {
        if (isEmpty()) {
            throw new RuntimeException("隊列為空,無法擷取資料!");
        }
        return arr[front + 1];
    }

    //列印隊列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("隊列為空!");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }
}

           

2. 數組模拟環形隊列

上述數組實作的隊列,隻能使用一次,無法真正實作取出資料的效果,是以對其進行優化,使用數組通過取模(%)操作實作環形隊列。
在環形隊列中,front始終為隊列第一個元素的索引;
而rear則始終為隊列最後一個元素的下一個索引;
是以,環形隊列最大容量(maxSize)為4時,如果隊列中裝了3個元素,則目前隊列已滿;
           

示例圖:

數組,隊列及環形隊列【Java實作】1. 數組模拟隊列2. 數組模拟環形隊列3. 測試類

示例代碼:

public class Queue {
    //隊列的最大容量
    private int maxSize;
    //隊列中第一個資料的索引
    private int front;
    //隊列中最後一個資料的索引
    private int rear;
    //模拟隊列的數組
    private int[] arr;

    //構造函數初始化隊列,因為int類型預設初始值為0,是以不需要給front和rear賦初值
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

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

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

    //往隊列中添加資料
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("隊列已滿,無法添加資料!");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    //從隊列中擷取資料
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("隊列為空,無法擷取資料!");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //擷取隊頭資料
    public int getHead() {
        if (isEmpty()) {
            throw new RuntimeException("隊列為空,無法擷取資料!");
        }
        return arr[front];
    }

    //列印隊列
    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("隊列為空!");
        }
        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;
    }
           

3. 測試類

測試類代碼:

public class Test {
    public static void main(String[] args) {
        CircleQueue arrayQueue = new CircleQueue(4);
        char key = ' ';
        Scanner in = 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):檢視隊列頭的資料");
            System.out.println("請選擇您要進行的操作(s/e/a/g/h):");
            key = in.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("請輸入一個資料:");
                    arrayQueue.addQueue(in.nextInt());
                    break;
                case 'g':
                    try {
                        int value = arrayQueue.getQueue();
                        System.out.printf("取出的資料為:%d\n", value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = arrayQueue.getHead();
                        System.out.printf("隊列頭的資料為:%d\n", head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    in.close();
                    loop = false;
                    System.out.println("程式退出!");
                    break;
                default:
                    break;
            }
        }
    }
}