Java編程內(nèi)功之怎么實(shí)現(xiàn)隊列,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)于2013年開始,先為港南等服務(wù)建站,港南等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為港南企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
基本介紹
隊列是一個有序列表,可以用數(shù)組或者鏈表來實(shí)現(xiàn)
遵循先入先出的原則,即先存入隊列的數(shù)據(jù),要先取出.后存入的要后取出
數(shù)組模擬隊列
隊列本身是有序列表,若使用數(shù)組的結(jié)構(gòu)來存儲隊列的數(shù)據(jù),則隊列數(shù)組的聲明如下圖,其中maxSize是該隊列的最大容量.
因?yàn)殛犃械妮斎隲輸出是分別從前后端來處理,因此需要兩個變量front及rear分別記錄隊列前后端的下標(biāo),front會隨著數(shù)據(jù)輸出而改變,而rear則是隨著數(shù)據(jù)輸入而改變.
代碼案例
package com.structures.queue; import java.util.Scanner; public class ArrayQueueDemo { public static void main(String[] args) { ArrayQueue arrayQueue = new ArrayQueue(3); char key = ' ';//接受用戶輸入 Scanner scanner = new Scanner(System.in); boolean loop = true; //輸出一個菜單 while (loop) { 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': arrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個整數(shù)"); int value = scanner.nextInt(); arrayQueue.addQueue(value); break; case 'g': try { int queue = arrayQueue.getQueue(); System.out.printf("取出的數(shù)據(jù)是%d", queue); }catch (Exception e){ System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; case 'h': try { int head = arrayQueue.headQueue(); System.out.printf("取出隊列頭的數(shù)據(jù)是%d", head); }catch (Exception e){ System.out.println(e.getMessage()); } default: break; } } System.out.println("程序退出"); } } //使用數(shù)組模擬隊列-編寫一個ArrayQueue類 class ArrayQueue { //表示數(shù)組最大容量 private int maxSize; //隊列頭 private int front; //隊列尾 private int rear; //用于存放數(shù)據(jù),模擬隊列 private int[] arr; //創(chuàng)建隊列構(gòu)造器 public ArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; front = -1;//指向隊列頭的前一個位置 rear = -1;//指向隊列尾的數(shù)據(jù),即就是隊列最后一個數(shù)據(jù) } //判斷隊列是否滿 public boolean isFull() { return rear == maxSize - 1; } //判斷隊列是否為空 public boolean isEmpty() { return rear == front; } //添加數(shù)據(jù)到隊列 public void addQueue(int n) { if (isFull()) { System.out.println("隊列不能加入數(shù)據(jù)"); return; } rear++;//讓rear 后移 arr[rear] = n; } //獲取隊列數(shù)據(jù),出隊列 public int getQueue() { if (isEmpty()) { throw new RuntimeException("隊列為空,不能取數(shù)據(jù)"); } front++; return arr[front]; } //顯示隊列所有數(shù)據(jù) public void showQueue() { if (isEmpty()) { System.out.println("隊列為空,沒有數(shù)據(jù)"); } for (int i = 0; i < this.arr.length; i++) { System.out.printf("arr[%d]=%d\n", i, arr[i]); } } //顯示隊列的頭數(shù)據(jù),注意不是取數(shù)據(jù) public int headQueue() { if (isEmpty()) { throw new RuntimeException("隊列為空,沒有數(shù)據(jù)"); } return arr[front + 1]; } }
問題分析
鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)
目前這個數(shù)組使用一次就不能用,沒有達(dá)到復(fù)用的效果.
將這個數(shù)組使用算法,改進(jìn)成一個環(huán)形的隊列:取模%
改進(jìn)成環(huán)形隊列的思路分析
鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)
front變量的含義做一個調(diào)整:front 就指向隊列的第一個元素,也就是arr[front]就是隊列的第一個元素,front的初始值=0
rear變量的含義做一個調(diào)整:rear 指向隊列的最后一個元素的后一個位置,因?yàn)橄M粘鲆粋€空間作為約定.rear初始值=0
當(dāng)隊列滿時,條件是(rear+1)%maxSize = front.
當(dāng)隊列為空時條件,rear == front 空.
當(dāng)我們這樣分析,隊列中有效的數(shù)據(jù)的個數(shù)=(rear+maxSize-front)%maxSize.
環(huán)形隊列代碼案例
package com.structures.queue; import java.util.Scanner; public class CircleArrayQueue { public static void main(String[] args) { CircleArray arrayQueue = new CircleArray(4);//這里設(shè)置4,其隊列的有效數(shù)據(jù)最大是3 char key = ' ';//接受用戶輸入 Scanner scanner = new Scanner(System.in); boolean loop = true; //輸出一個菜單 while (loop) { 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': arrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個整數(shù)"); int value = scanner.nextInt(); arrayQueue.addQueue(value); break; case 'g': try { int queue = arrayQueue.getQueue(); System.out.printf("取出的數(shù)據(jù)是%d", queue); }catch (Exception e){ System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; case 'h': try { int head = arrayQueue.headQueue(); System.out.printf("取出隊列頭的數(shù)據(jù)是%d", head); }catch (Exception e){ System.out.println(e.getMessage()); } default: break; } } System.out.println("程序退出"); } } class CircleArray { //表示數(shù)組最大容量 private int maxSize; //front變量的含義做一個調(diào)整:front 就指向隊列的第一個元素,也就是arr[front]就是隊列的第一個元素,front的初始值=0 private int front; //rear變量的含義做一個調(diào)整:rear 指向隊列的最后一個元素的后一個位置,因?yàn)橄M粘鲆粋€空間作為約定.rear初始值=0 private int rear; //用于存放數(shù)據(jù),模擬隊列 private int[] arr; public CircleArray(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } //判斷隊列是否滿 public boolean isFull() { return (rear + 1) % maxSize == front; } //判斷隊列是否為空 public boolean isEmpty() { return rear == front; } //添加數(shù)據(jù)到隊列 public void addQueue(int n) { if (isFull()) { System.out.println("隊列滿,隊列不能加入數(shù)據(jù)"); return; } //直接將數(shù)據(jù)加入 arr[rear] = n; //將rear后移,這里必須考慮取模 rear = (rear + 1) % maxSize; } //獲取隊列數(shù)據(jù),出隊列 public int getQueue() { if (isEmpty()) { throw new RuntimeException("隊列為空,不能取數(shù)據(jù)"); } //這里需要分析front是指向隊列的第一個元素, //1.先把front對應(yīng)的值保存到一個臨時變量, //2.將front后移,考慮取模 //3.將臨時保存的變量返回 int value = arr[front]; front = (front + 1) % maxSize; return value; } //顯示隊列所有數(shù)據(jù) public void showQueue() { if (isEmpty()) { System.out.println("隊列為空,沒有數(shù)據(jù)"); } //從front開始遍歷 for (int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]); } } //求出當(dāng)前隊列有效數(shù)據(jù)的個數(shù) public int size() { return (rear + maxSize - front) % maxSize; } //顯示隊列的頭數(shù)據(jù),注意不是取數(shù)據(jù) public int headQueue() { if (isEmpty()) { throw new RuntimeException("隊列為空,沒有數(shù)據(jù)"); } return arr[front]; } }
看完上述內(nèi)容,你們掌握J(rèn)ava編程內(nèi)功之怎么實(shí)現(xiàn)隊列的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:Java編程內(nèi)功之怎么實(shí)現(xiàn)隊列
瀏覽地址:http://www.rwnh.cn/article8/phooip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站營銷、外貿(mào)建站、標(biāo)簽優(yōu)化、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)