隊列簡介
隊列是一個有序列表,可以用數(shù)組或是鏈表來實現(xiàn)。
遵循先入先出的原則。即先存入隊列的數(shù)據(jù),先取出,后存入的后取出。
示意圖:(使用數(shù)組模擬隊列示意圖)
有兩個分別指向頭部和尾部的“指針”。
數(shù)組模擬隊列(無法復(fù)用)
1、實現(xiàn)思路
隊列本身是有序列表,若使用數(shù)組的結(jié)構(gòu)來存儲隊列的數(shù)據(jù),則隊列數(shù)組的聲明如下圖,其中maxSize是該隊列的最大容量。
因為隊列的輸出、輸入是分別從前后端來處理,因此需要兩個變量front及rear分別記錄隊列前后端的下標(biāo),front會隨著數(shù)據(jù)輸出而改變,而rear則是隨著數(shù)據(jù)輸入而改變,如圖所示:
當(dāng)我們將數(shù)據(jù)存入隊列時稱為addQueue,addQueue的處理需要有兩個步驟:
①將尾指針往后移。
②若尾指針rear小于隊列的最大下標(biāo)maxSize-1,則將數(shù)據(jù)存入rear 所指的數(shù)組元素中,否則無法存入數(shù)據(jù)。
rear+1當(dāng)front== rear[空]
rear==maxSize-1[隊列滿]
2、代碼實現(xiàn)
①數(shù)組實現(xiàn)隊列類
class ArrQueue { private int maxSize; //隊列(數(shù)組)最大容量 private int front; //指向隊列頭部 private int rear; //指向隊列尾部 private int[] queue; //創(chuàng)造隊列的構(gòu)造器 public ArrQueue(int maxSize){ this.maxSize = maxSize; queue = new int[maxSize]; front = -1; //其實是隊列第一個元素的前一個索引 rear = -1; //最后一個元素的索引 } //判斷是否滿 public boolean isFull(){ return rear == maxSize - 1; } //判斷是否空 public boolean isEmpty(){ return front == rear; } //添加元素 public void addQueue(int n){ if (isFull()){ System.out.println("隊列已經(jīng)滿了,無法添加!"); return; }else { rear++; queue[rear] = n; } } //取出元素 public int getQueue(){ if (isEmpty()){ throw new RuntimeException("隊列為空,無元素可取!"); }else { front++; return queue[front]; } } //顯示隊列 public void showQueue(){ if (isEmpty()){ System.out.println("隊列為空,沒有元素可顯示!"); return; } for (int i : queue){ System.out.println(i); } } //顯示頭數(shù)據(jù) public void headQueue(){ if (isEmpty()){ throw new RuntimeException("隊列為空,沒有頭數(shù)據(jù)!"); } int i = front; System.out.println(queue[++i]); } }
②測試類
import java.util.Scanner; /** * @Author: Yeman * @Date: 2021-10-11-22:02 * @Description: */ public class ArrayQueueTest { public static void main(String[] args) { //創(chuàng)建一個隊列 ArrQueue arrQueue = new ArrQueue(3); //創(chuàng)建一個用戶輸入 Scanner scanner = new Scanner(System.in); //創(chuàng)建一個功能菜單 char key = " "; boolean isShow = true; while (isShow){ System.out.println("s:顯示隊列"); System.out.println("a:添加數(shù)據(jù)"); System.out.println("g:取出數(shù)據(jù)"); System.out.println("h:顯示頭數(shù)據(jù)"); System.out.println("e:退出程序"); key = scanner.next().charAt(0); switch (key){ case "s" : arrQueue.showQueue(); break; case "a" : System.out.println("請輸入一個數(shù):"); int value = scanner.nextInt(); arrQueue.addQueue(value); break; case "g" : try { System.out.println(arrQueue.getQueue()); } catch (Exception e) { e.printStackTrace(); } break; case "h" : try { arrQueue.headQueue(); } catch (Exception e) { e.printStackTrace(); } break; case "e" : isShow = false; break; } } System.out.println("程序退出..."); } }
數(shù)組模擬環(huán)形隊列(可復(fù)用)
對前面的數(shù)組模擬隊列的優(yōu)化,充分利用數(shù)組。將數(shù)組看做是一個環(huán)形的,即取出之后,有位置可以空出來添加。(通過取模的方式來實現(xiàn)即可)
分析說明:
①尾索引的下一個為頭索引時表示隊列滿,即將隊列容量空出一個作為約定。在作判斷隊列滿的時候需要注意(rear+ 1) % maxSize== front [滿]
②rear == front [空]
1、思路如下:
①front 變量的含義調(diào)整:front 指向隊列的第一個元素, 也就是說arr[front]就是隊列的第一個元素,front的初始值為0。
②rear 變量的含義調(diào)整:rear 指向隊列的最后一個元素的后一個位置,因為希望空出一個空間做為約定,rear的初始值=0。
③當(dāng)隊列滿時,條件是(rear + 1) % maxSize == front
[滿]
④對隊列為空的條件是rear== front
[空]
⑤當(dāng)我們這樣分析,隊列中有效的數(shù)據(jù)的個數(shù)(rear + maxSize - front) % maxSize
⑥我們就可以在原來的隊列上修改得到一個環(huán)形隊列
2、代碼實現(xiàn)
①數(shù)組實現(xiàn)環(huán)形隊列類
class ArrQueue { private int maxSize; //隊列(數(shù)組)最大容量 private int front; //指向隊列頭部,隊列第一個元素的索引 private int rear; //指向隊列尾部,隊列最后一個元素的后一個索引 private int[] queue; //創(chuàng)造隊列的構(gòu)造器 public ArrQueue(int maxSize){ this.maxSize = maxSize; queue = new int[maxSize]; } //判斷是否滿 public boolean isFull(){ return (rear + 1) % maxSize == front; } //判斷是否空 public boolean isEmpty(){ return front == rear; } //添加元素 public void addQueue(int n){ if (isFull()){ System.out.println("隊列已經(jīng)滿了,無法添加!"); return; }else { queue[rear] = n; rear = (rear + 1) % maxSize; } } //取出元素 public int getQueue(){ if (isEmpty()){ throw new RuntimeException("隊列為空,無元素可取!"); }else { int data = queue[front]; front = (front + 1) % maxSize; return data; } } //顯示隊列 public void showQueue(){ if (isEmpty()){ System.out.println("隊列為空,沒有元素可顯示!"); return; } for (int i = front; i < front + size(); i++) { System.out.printf("arr[%d] = %d ",i % maxSize,queue[i % maxSize]); } } //求當(dāng)前隊列有效數(shù)據(jù)個數(shù) public int size(){ return (rear + maxSize - front) % maxSize; } //顯示頭數(shù)據(jù) public void headQueue(){ if (isEmpty()){ throw new RuntimeException("隊列為空,沒有頭數(shù)據(jù)!"); } System.out.println(queue[front]); } }
②測試類
import java.util.Scanner; /** * @Author: Yeman * @Date: 2021-10-11-22:02 * @Description: */ public class ArrayQueueTest { public static void main(String[] args) { //創(chuàng)建一個隊列 ArrQueue arrQueue = new ArrQueue(3); //說明該環(huán)形隊列的最大有效數(shù)據(jù)為2 //創(chuàng)建一個用戶輸入 Scanner scanner = new Scanner(System.in); //創(chuàng)建一個功能菜單 char key = " "; boolean isShow = true; while (isShow){ System.out.println("s:顯示隊列"); System.out.println("a:添加數(shù)據(jù)"); System.out.println("g:取出數(shù)據(jù)"); System.out.println("h:顯示頭數(shù)據(jù)"); System.out.println("e:退出程序"); key = scanner.next().charAt(0); switch (key){ case "s" : arrQueue.showQueue(); break; case "a" : System.out.println("請輸入一個數(shù):"); int value = scanner.nextInt(); arrQueue.addQueue(value); break; case "g" : try { System.out.println(arrQueue.getQueue()); } catch (Exception e) { e.printStackTrace(); } break; case "h" : try { arrQueue.headQueue(); } catch (Exception e) { e.printStackTrace(); } break; case "e" : isShow = false; break; } } System.out.println("程序退出..."); } }
到此這篇關(guān)于Java隊列篇之實現(xiàn)數(shù)組模擬隊列及可復(fù)用環(huán)形隊列詳解的文章就介紹到這了,更多相關(guān)Java 隊列內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/m0_46653805/article/details/120712729