java數據結構之環形隊列

底層數組實現了一個環形隊列

 

package cn.agan.queue;

import java.util.Scanner;

/**
 * 使用數組模擬隊列
 */
public class CircularQueueDemo {
    public static void main(String[] args) {
        //創建一個隊列
        CircularQueue arrayQueue = new CircularQueue(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):查看隊列頭數據");
            System.out.println("l(large):查看隊列當前包含多少個元素");
            System.out.println("=====================");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();

                    break;
                case 'a':
                    System.out.println("輸入一個數據");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);

                    break;
                case 'g':
                    try {
                        int pop = arrayQueue.pop();
                        System.out.println("獲取到的數據爲"+pop);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'h':
                    try {
                        int pop = arrayQueue.peakQueue();
                        System.out.println("隊列頭的數據爲"+pop);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'l':
                    System.out.println("當前隊列的元素個數爲:"+arrayQueue.queueSize());
                    break;
                default:
                    System.out.println("尚未支持的操作");
            }
            System.out.println();
        }

        System.out.println("程序退出");
    }
}


class CircularQueue {
    private int maxSize ; //表示數組的最大容量
    private int front; //指向隊列的第一個元素位置,初始值爲0
    private int rear; //隊列最後一個元素的下一個位置,初始值爲0
    private int[] arr; //用於存放數據的數組

    //創建隊列的構造器
    public CircularQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = 0;  //指向隊列前面一個位置
        rear = 0;   //指向隊列尾的位置,隊列最後一個元素+1
    }
    //判斷隊列是否滿
    public boolean isFull() {
        return (rear + 1 + maxSize) % maxSize == front;
    }

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

    //添加數據到隊列
    public void addQueue(int e) {
        if ( !isFull() ) {
            arr[rear] = e;
            rear = ++rear % maxSize;
        } else {
            System.out.printf("queue is full add failed");
            return;
        }
    }

    //獲取數據
    public int pop() {
        if (isEmpty()) {
            //拋出異常
            throw new RuntimeException("隊列空,不能取出數據");
        }

        int e = arr[front];
        front = ++front % maxSize;

        return e;
    }

    //顯示隊列的所有數據
    public void showQueue() {
        if (isEmpty()) {
            System.out.printf("隊列爲空,沒有數據。");
            return;
        }

        int cnt = queueSize();
        int index = 0;
        for (int i = front; i < front+cnt; i++) {
            index = i % maxSize;
            System.out.printf("%d\t", arr[index]);
        }
        System.out.println();
    }

    //顯示隊列的頭
    public int peakQueue() {
        if (isEmpty()) {
            throw new RuntimeException("隊列沒有數據");
        }

        return arr[front];
    }
    //查詢當前隊列一共有多少個元素
    public int queueSize() {
       return (rear + maxSize - front) % maxSize;
    }
}

 

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