(数据结构与算法)数组模拟队列和环形队列
生活随笔
收集整理的這篇文章主要介紹了
(数据结构与算法)数组模拟队列和环形队列
小編覺得挺不錯(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ù)組模擬環(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsp中获取不到后台请求域中的值
- 下一篇: (数据结构与算法)单链表与双链表增删改查