1. 程式人生 > 實用技巧 >稀疏陣列和佇列 —— 佇列

稀疏陣列和佇列 —— 佇列

佇列介紹

  1. 佇列是一個有序列表,可以用陣列或是連結串列來實現。
  2. 遵循先入先出的原則。即:先存入佇列的資料,要先取出。後存入的要後取出
  3. 示意圖:(使用陣列模擬佇列示意圖)

陣列模擬佇列思路

  1. 佇列本身是有序列表,若使用陣列的結構來儲存佇列的資料,則佇列陣列的宣告如下圖, 其中 maxSize 是該隊 列的最大容量。

  2. 因為佇列的輸出、輸入是分別從前後端來處理,因此需要兩個變數 front 及 rear 分別記錄佇列前後端的下標, front 會隨著資料輸出而改變,而 rear 則是隨著資料輸入而改變,如圖所示:

  3. 當我們將資料存入佇列時稱為”addQueue”,addQueue 的處理需要有兩個步驟:思路分析 1) 將尾指標往後移:rear+1 , 當 front == rear 【空】 2) 若尾指標 rear 小於佇列的最大下標 maxSize-1,則將資料存入 rear 所指的陣列元素中,否則無法存入資料。 rear == maxSize - 1[佇列滿]

程式碼實現:

package com.yiyang.myfirstspringdemo.algotithm;

import java.util.Scanner;

/**
 * @Author 劉翊揚
 * @Date 2020/11/22 下午6:43
 * @Version 1.0
 */
public class ArrayQueueDemo {

    public static void main(String[] args) {
        //測試一把 //建立一個佇列
        MyArrayQueue queue = new MyArrayQueue(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;
                    }
                    break;
                case 'h': //檢視佇列頭的資料
                    try {
                        int res = queue.peek();
                        System.out.printf("佇列頭的資料是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception System.out.println(e.getMessage()); }break;
                    }
                    break;
                case 'e': //退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}

class MyArrayQueue {

    private int maxSize; // 佇列的最大容量

    private int front; // 佇列頭

    private int rear; // 佇列尾部

    private int arr[];

    public MyArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.rear = -1;
        this.front = -1;
        this.arr = new int[maxSize];
    }

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

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

    // 新增資料到佇列中
    public void addQueue(int n) {
        if (isFull()) {
            System.out.printf("佇列已經滿了,無法繼續新增");
            return;
        }
        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 peek() {
        if (isEmpty()) {
            throw new RuntimeException("佇列為空,沒有資料");
        }
        return arr[front + 1];
    }

}

存在的問題?

  1. 目前陣列只能用一次,沒有達到複用的效果。
  2. 將這個陣列使用演算法,改進成一個環形的佇列。取模:%