(数据结构与算法)数组模拟队列和环形队列
生活随笔
收集整理的這篇文章主要介紹了
(数据结构与算法)数组模拟队列和环形队列
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 數(shù)組模擬隊列
- 思路
- 代碼實現(xiàn)
- 問題分析并優(yōu)化
- 數(shù)組模擬環(huán)形隊列
- 思路
- 代碼實現(xiàn)
數(shù)組模擬隊列
- 隊列是一個有序列表,可以用數(shù)組或是鏈表來實現(xiàn)。
- 遵循先入先出的原則。即:先存入隊列的數(shù)據(jù),要先取出。后存入的要后取出
- 示意圖: (使用數(shù)組模擬隊列示意圖)
思路
- maxSize為隊列最大容量
- front和rear記錄隊列前后端的下標
- front隨著隊列輸出而改變,rear隨著隊列輸入而改變
- 當(dāng)front = rear時,隊列為空
- 當(dāng)rear = maxSize -1 時,隊列滿
代碼實現(xiàn)
public class Demo02ArrayQueue {public static void main(String[] args) {//測試//創(chuàng)建一個隊列ArrayQueue queue = new ArrayQueue(3);//設(shè)置隊列最大值為3boolean flag = true;char key = ' ';//用于接收輸入字符Scanner scanner = new Scanner(System.in);while (flag){System.out.println("請輸入相應(yīng)的命令操作隊列");System.out.println("s(show):顯示隊列");System.out.println("e(exit):退出程序");System.out.println("a(add):添加數(shù)據(jù)到隊列");System.out.println("g(get):從隊列中取出數(shù)據(jù)");System.out.println("h(head):查看隊列頭的數(shù)據(jù)");key= scanner.next().charAt(0);switch (key){case 's':queue.showQueue();break;case 'e':flag=false;break;case 'a':System.out.println("輸入一個數(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ù)組模擬隊列,編寫一個ArrayQueue類 class ArrayQueue{private int maxSize;//隊列最大值private int font;//隊列頭,指向隊列頭數(shù)據(jù)的前一個位置private int rear;//隊列尾,指向隊列尾數(shù)據(jù)本身下標private int[] arr;//存放隊列的數(shù)組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;}//添加數(shù)據(jù)到隊列,n為添加的數(shù)據(jù)public void addQueue( int n){if (isFull()){System.out.println("隊列已滿,無法添加數(shù)據(jù)到隊列");}else {rear++;arr[rear] = n;}}//從隊列中取出數(shù)據(jù)public int getQueue(){if (isEmpty()){//拋出異常throw new RuntimeException("隊列空,不能取出數(shù)據(jù)");}else {font++;//返回被取出的數(shù)據(jù)return arr[font];}}//顯示隊列public void showQueue(){if (isEmpty()){System.out.println("隊列為空!");}else {//font為隊頭的前一個下標,所以font+1就是頭數(shù)據(jù)的下標for (int i = font+1; i <rear+1 ; i++) {System.out.println(arr[i]);}}}public void showHead(){if (isEmpty()){throw new RuntimeException("隊列空,不能取出數(shù)據(jù)");}else {System.out.println("隊列頭數(shù)據(jù)為"+arr[font+1]);}} }問題分析并優(yōu)化
數(shù)組模擬環(huán)形隊列
思路
對前面的數(shù)組模擬隊列的優(yōu)化,充分利用數(shù)組.因此將數(shù)組看做是一個環(huán)形的。(通過取模的方式來實現(xiàn)即可)
注意點:
- front表示隊列頭本身下標,rear表示隊列尾下一個數(shù)據(jù)的下標,初始值都為0
- (rear+1)%maxSize == front判斷隊列是否已滿
- 添加元素 rear=(rear+1)%maxSize,防止下標越界 任何數(shù)取余不會超過除數(shù)。獲取元素也一樣。
- size()返回隊列中有效數(shù)據(jù)的個數(shù) (rear+maxSize - front)%maxSize,+maxSize使rear-front不會為負數(shù)
- 遍歷時 i%maixsize 不會越界
代碼實現(xiàn)
public class Demo03CircleArrayQueue {public static void main(String[] args) {//測試//創(chuàng)建一個隊列CircleArrayQueue queue = new CircleArrayQueue(4);//設(shè)置隊列最大值為3boolean flag = true;char key = ' ';//用于接收輸入字符Scanner scanner = new Scanner(System.in);while (flag){System.out.println("請輸入相應(yīng)的命令操作隊列");System.out.println("s(show):顯示隊列");System.out.println("e(exit):退出程序");System.out.println("a(add):添加數(shù)據(jù)到隊列");System.out.println("g(get):從隊列中取出數(shù)據(jù)");System.out.println("h(head):查看隊列頭的數(shù)據(jù)");key= scanner.next().charAt(0);switch (key){case 's':queue.showQueue();break;case 'e':flag=false;break;case 'a':System.out.println("輸入一個數(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ù)組模擬隊列,編寫一個ArrayQueue類 class CircleArrayQueue{private int maxSize;//隊列最大值private int font;//隊列頭,默認為0,指向隊列頭數(shù)據(jù)的下標private int rear;//隊列尾,默認為0,指向隊列尾數(shù)據(jù)的后一個數(shù)據(jù)的下標private int[] arr;//存放隊列的數(shù)組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;}//添加數(shù)據(jù)到隊列,n為添加的數(shù)據(jù)public void addQueue( int n){if (isFull()){System.out.println("隊列已滿,無法添加數(shù)據(jù)到隊列");}else {arr[rear] = n;//防止rear越界,任何數(shù)取余不會超過除數(shù)。//將rear后移rear = (rear+1)%maxSize;}}//從隊列中取出數(shù)據(jù)public int getQueue(){if (isEmpty()){//拋出異常throw new RuntimeException("隊列空,不能取出數(shù)據(jù)");}else {int value = arr[font];//防止font越界,任何數(shù)取余不會超過除數(shù)。//將font后移font = (font+1)%maxSize;//返回被取出的數(shù)據(jù)return value;}}//顯示隊列public void showQueue(){if (isEmpty()){System.out.println("隊列為空!");}else {//font為隊頭的前一個下標,所以font+1就是頭數(shù)據(jù)的下標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("隊列空,不能取出數(shù)據(jù)");}else {System.out.println("隊列頭數(shù)據(jù)為"+arr[font]);}} }總結(jié)
以上是生活随笔為你收集整理的(数据结构与算法)数组模拟队列和环形队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsp中获取不到后台请求域中的值
- 下一篇: (数据结构与算法)单链表与双链表增删改查