算法——【隊列】——用數組模擬隊列

隊列


什麼是隊列

說到隊列,自然會想到現實中說的隊列,例如“打飯排隊”,“車隊”,他們都是從一端進行入隊,也就是排隊的隊尾,一端進行出隊,也就是排隊的隊頭,而且是先入隊的先出隊。

隊列的特點

先進先出,一端進一端出。

隊列的使用場景

①樹的層次遍歷
②圖的廣度優先遍歷(DFS)
③操作系統中的先來先服務原則
以上三個都是隊列常見的使用場景

代碼實現

用數組模擬一個隊列

package com.practise.arrayqueue;

import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.Scanner;

public class ArrayQueue {
    //隊列數據數組
    int  arr[];
    //頭指針
    int front;
    //尾指針
    int rear;
    //隊列最大長度
    int maxSize;

    //構造函數,傳入隊列最大值
    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

    //判斷隊滿
    public boolean isFull(){
        //隊尾指針=maxsize-1
        return rear == maxSize - 1;
    }

    //判斷隊空
    public boolean isEmpty(){
        //隊尾=隊頭
        return front == rear;
    }

    //入隊操作
    public void addQueue(int n){
        //判斷隊列是否滿
        if(isFull()){
            System.out.println("隊列滿,不能加入數據!");
            return;
        }
        //讓rear後移
        rear++;
        //隊尾處賦值
        arr[rear] = n;
    }

    //獲取隊列數據
    public int getQueue(){
        //判斷隊空
        if(isEmpty()){
            throw new RuntimeException("隊列空,不能取數據!");
        }
        //front後移
        front++;
        return arr[front];
    }

    //顯示所有隊列數據
    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]);
        }
    }

    //顯示隊頭數據
    public int headQueue(){
        //判空
        if(isEmpty()){
            throw new RuntimeException("隊列空,不能取數據!");
        }
        return arr[front + 1];
    }

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(10);
        //用戶輸入
        char key = ' ';
        //創建Scanner
        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':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("請輸入一個數");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();;
                        System.out.printf("取出的數據是:%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("頭部的數據是:%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出-----");
    }
}

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