日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (数据结构与算法)数组模拟队列和环形队列 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 數(shù)組模擬隊(duì)列
    • 思路
    • 代碼實(shí)現(xiàn)
    • 問題分析并優(yōu)化
  • 數(shù)組模擬環(huán)形隊(duì)列
    • 思路
    • 代碼實(shí)現(xiàn)

數(shù)組模擬隊(duì)列

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

思路

  • maxSize為隊(duì)列最大容量
  • front和rear記錄隊(duì)列前后端的下標(biāo)
  • front隨著隊(duì)列輸出而改變,rear隨著隊(duì)列輸入而改變
  • 當(dāng)front = rear時(shí),隊(duì)列為空
  • 當(dāng)rear = maxSize -1 時(shí),隊(duì)列滿

代碼實(shí)現(xiàn)

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

問題分析并優(yōu)化

  • 數(shù)組使用一次就不能用了,沒有達(dá)到復(fù)用的效果
  • 將這個(gè)數(shù)組使用算法,改進(jìn)成一個(gè)環(huán)形隊(duì)列,取模:%
  • 數(shù)組模擬環(huán)形隊(duì)列

    思路

    對(duì)前面的數(shù)組模擬隊(duì)列的優(yōu)化,充分利用數(shù)組.因此將數(shù)組看做是一個(gè)環(huán)形的。(通過取模的方式來實(shí)現(xiàn)即可)


    注意點(diǎn):

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

    代碼實(shí)現(xiàn)

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

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。