日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

(数据结构与算法)数组模拟队列和环形队列

發布時間:2025/3/20 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (数据结构与算法)数组模拟队列和环形队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 數組模擬隊列
    • 思路
    • 代碼實現
    • 問題分析并優化
  • 數組模擬環形隊列
    • 思路
    • 代碼實現

數組模擬隊列

  • 隊列是一個有序列表,可以用數組或是鏈表來實現。
  • 遵循先入先出的原則。即:先存入隊列的數據,要先取出。后存入的要后取出
  • 示意圖: (使用數組模擬隊列示意圖)

思路

  • maxSize為隊列最大容量
  • front和rear記錄隊列前后端的下標
  • front隨著隊列輸出而改變,rear隨著隊列輸入而改變
  • 當front = rear時,隊列為空
  • 當rear = maxSize -1 時,隊列滿

代碼實現

public class Demo02ArrayQueue {public static void main(String[] args) {//測試//創建一個隊列ArrayQueue queue = new ArrayQueue(3);//設置隊列最大值為3boolean flag = true;char key = ' ';//用于接收輸入字符Scanner scanner = new Scanner(System.in);while (flag){System.out.println("請輸入相應的命令操作隊列");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 'e':flag=false;break;case 'a':System.out.println("輸入一個數:");int value = scanner.nextInt();queue.addQueue(value);break;case 'g':try {int res = queue.getQueue();System.out.println("被取出的數據是"+res);}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try {queue.showHead();}catch (Exception e){System.out.println(e.getMessage());}break;}}} } //使用數組模擬隊列,編寫一個ArrayQueue類 class ArrayQueue{private int maxSize;//隊列最大值private int font;//隊列頭,指向隊列頭數據的前一個位置private int rear;//隊列尾,指向隊列尾數據本身下標private int[] arr;//存放隊列的數組public ArrayQueue() {}public ArrayQueue(int maxSize) {this.maxSize = maxSize;arr = new int[maxSize];font=-1;rear=-1;}//判斷隊列是否滿public boolean isFull(){return rear == maxSize-1;}//判斷隊列是否為空public boolean isEmpty(){return rear == font;}//添加數據到隊列,n為添加的數據public void addQueue( int n){if (isFull()){System.out.println("隊列已滿,無法添加數據到隊列");}else {rear++;arr[rear] = n;}}//從隊列中取出數據public int getQueue(){if (isEmpty()){//拋出異常throw new RuntimeException("隊列空,不能取出數據");}else {font++;//返回被取出的數據return arr[font];}}//顯示隊列public void showQueue(){if (isEmpty()){System.out.println("隊列為空!");}else {//font為隊頭的前一個下標,所以font+1就是頭數據的下標for (int i = font+1; i <rear+1 ; i++) {System.out.println(arr[i]);}}}public void showHead(){if (isEmpty()){throw new RuntimeException("隊列空,不能取出數據");}else {System.out.println("隊列頭數據為"+arr[font+1]);}} }

問題分析并優化

  • 數組使用一次就不能用了,沒有達到復用的效果
  • 將這個數組使用算法,改進成一個環形隊列,取模:%
  • 數組模擬環形隊列

    思路

    對前面的數組模擬隊列的優化,充分利用數組.因此將數組看做是一個環形的。(通過取模的方式來實現即可)


    注意點:

    • front表示隊列頭本身下標,rear表示隊列尾下一個數據的下標,初始值都為0
    • (rear+1)%maxSize == front判斷隊列是否已滿
    • 添加元素 rear=(rear+1)%maxSize,防止下標越界 任何數取余不會超過除數。獲取元素也一樣。
    • size()返回隊列中有效數據的個數 (rear+maxSize - front)%maxSize,+maxSize使rear-front不會為負數
    • 遍歷時 i%maixsize 不會越界

    代碼實現

    public class Demo03CircleArrayQueue {public static void main(String[] args) {//測試//創建一個隊列CircleArrayQueue queue = new CircleArrayQueue(4);//設置隊列最大值為3boolean flag = true;char key = ' ';//用于接收輸入字符Scanner scanner = new Scanner(System.in);while (flag){System.out.println("請輸入相應的命令操作隊列");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 'e':flag=false;break;case 'a':System.out.println("輸入一個數:");int value = scanner.nextInt();queue.addQueue(value);break;case 'g':try {int res = queue.getQueue();System.out.println("被取出的數據是"+res);}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try {queue.showHead();}catch (Exception e){System.out.println(e.getMessage());}break;}}} }//使用數組模擬隊列,編寫一個ArrayQueue類 class CircleArrayQueue{private int maxSize;//隊列最大值private int font;//隊列頭,默認為0,指向隊列頭數據的下標private int rear;//隊列尾,默認為0,指向隊列尾數據的后一個數據的下標private int[] arr;//存放隊列的數組public CircleArrayQueue() {}public CircleArrayQueue(int maxSize) {this.maxSize = maxSize;arr = new int[maxSize];}//判斷隊列是否滿public boolean isFull(){return (rear+1)%maxSize==font;}//判斷隊列是否為空public boolean isEmpty(){return rear == font;}//添加數據到隊列,n為添加的數據public void addQueue( int n){if (isFull()){System.out.println("隊列已滿,無法添加數據到隊列");}else {arr[rear] = n;//防止rear越界,任何數取余不會超過除數。//將rear后移rear = (rear+1)%maxSize;}}//從隊列中取出數據public int getQueue(){if (isEmpty()){//拋出異常throw new RuntimeException("隊列空,不能取出數據");}else {int value = arr[font];//防止font越界,任何數取余不會超過除數。//將font后移font = (font+1)%maxSize;//返回被取出的數據return value;}}//顯示隊列public void showQueue(){if (isEmpty()){System.out.println("隊列為空!");}else {//font為隊頭的前一個下標,所以font+1就是頭數據的下標for (int i = font; i <size() ; i++) {//防止i%maxSize下標越界System.out.println(arr[i%maxSize]);}}}public int size(){return (rear+maxSize-font)%maxSize;}public void showHead(){if (isEmpty()){throw new RuntimeException("隊列空,不能取出數據");}else {System.out.println("隊列頭數據為"+arr[font]);}} }

    總結

    以上是生活随笔為你收集整理的(数据结构与算法)数组模拟队列和环形队列的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。