Java循環(huán)隊(duì)列與非循環(huán)隊(duì)列的區(qū)別總結(jié)
非循環(huán)循環(huán)隊(duì)列
- 判滿:(rear+1) % maxsize == front
- 判空:front == rear
- 隊(duì)列元素個(gè)數(shù):rear = (rear + maxsize - front) % maxsize
- front指針移動(dòng)方式:front = (front + 1) % maxsize
- rear指針移動(dòng)方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //創(chuàng)建隊(duì)列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop) { 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': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個(gè)數(shù)"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的數(shù)據(jù)是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("隊(duì)列頭的數(shù)據(jù)是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //隊(duì)列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //構(gòu)造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; arr = new int[maxsize]; } //判滿 public boolean isFull() { return (rear+1)%maxsize == front; } //判空 public boolean isEmpty() { return rear == front; } //入隊(duì) public void addQueue(int n) { if(isFull()) { System.out.println("隊(duì)列已滿,不能再添加!"); return; } //添加數(shù)據(jù) arr[rear] = n; //rear后移 rear = (rear + 1) % maxsize; } //出隊(duì) public int getQueue() { if(isEmpty()) { throw new RuntimeException("隊(duì)列為空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍歷 public void showQueue() { if(isEmpty()) { System.out.println("隊(duì)列為空!"); return; } for(int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]); } } //求隊(duì)列有效數(shù)據(jù)的個(gè)數(shù) public int size() { return (rear + maxsize - front) % maxsize; } //顯示隊(duì)頭元素 public int headQueue() { if(isEmpty()) { throw new RuntimeException("隊(duì)列為空,不可取出元素!"); } return arr[front]; }
結(jié)果示意圖
循環(huán)隊(duì)列
- 判滿:(rear+1) % maxsize == front
- 判空:front == rear
- 隊(duì)列元素個(gè)數(shù):rear = (rear + maxsize - front) % maxsize
- front 指針移動(dòng)方式:front = (front + 1) % maxsizer
- ear指針移動(dòng)方式:rear= (rear+ 1) % maxsize
import java.awt.Font; import java.util.Scanner; import javax.management.RuntimeErrorException; public class CircleArrayQueueDemo { public static void main(String[] args) { //創(chuàng)建隊(duì)列 CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop) { 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': circleArrayQueue.showQueue(); break; case 'e': circleArrayQueue.showQueue(); break; case 'a': System.out.println("輸入一個(gè)數(shù)"); int value = scanner.nextInt(); circleArrayQueue.addQueue(value); break; case 'g': try { int res = circleArrayQueue.getQueue(); System.out.printf("取出的數(shù)據(jù)是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = circleArrayQueue.headQueue(); System.out.printf("隊(duì)列頭的數(shù)據(jù)是%d\n",res); }catch (Exception e) { System.out.println(e.getMessage()); } break; default: scanner.close(); loop = false; break; } } System.out.println("程序退出"); } } //隊(duì)列 class CircleArrayQueue{ private int maxsize; private int front; private int rear; private int[] arr; //構(gòu)造器 public CircleArrayQueue(int arrmaxsize) { maxsize = arrmaxsize; front = 0; rear = 0; arr = new int[maxsize]; } //判滿 public boolean isFull() { return (rear+1)%maxsize == front; } //判空 public boolean isEmpty() { return rear == front; } //入隊(duì) public void addQueue(int n) { if(isFull()) { System.out.println("隊(duì)列已滿,不能再添加!"); return; } //添加數(shù)據(jù) arr[rear] = n; //rear后移 rear = (rear + 1) % maxsize; } //出隊(duì) public int getQueue() { if(isEmpty()) { throw new RuntimeException("隊(duì)列為空,不可取出元素!"); } //取值 int value = arr[front]; //front后移 front = (front + 1)%maxsize; return value; } //遍歷 public void showQueue() { if(isEmpty()) { System.out.println("隊(duì)列為空!"); return; } for(int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n",i % maxsize, arr[i % maxsize]); } } //求隊(duì)列有效數(shù)據(jù)的個(gè)數(shù) public int size() { return (rear + maxsize - front) % maxsize; } //顯示隊(duì)頭元素 public int headQueue() { if(isEmpty()) { throw new RuntimeException("隊(duì)列為空,不可取出元素!"); } return arr[front]; } }
結(jié)果示意圖
到此這篇關(guān)于Java循環(huán)隊(duì)列與非循環(huán)隊(duì)列的區(qū)別總結(jié)的文章就介紹到這了,更多相關(guān)Java循環(huán)隊(duì)列與非循環(huán)隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列示例介紹
- Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)
- Java代碼實(shí)現(xiàn)循環(huán)隊(duì)列的示例代碼
- java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):順序隊(duì)列和循環(huán)隊(duì)列
- Java動(dòng)態(tài)循環(huán)隊(duì)列是如何實(shí)現(xiàn)的
- Java基礎(chǔ)之?dāng)?shù)組模擬循環(huán)隊(duì)列
- Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列
- Java循環(huán)隊(duì)列原理與用法詳解
- Java?循環(huán)隊(duì)列/環(huán)形隊(duì)列的實(shí)現(xiàn)流程
相關(guān)文章
mybatis typeAliases 給實(shí)體類起別名的方法
這篇文章主要介紹了mybatis typeAliases 給實(shí)體類起別名,本文給大家分享兩種用法,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java反射中java.beans包學(xué)習(xí)總結(jié)
本篇文章通過學(xué)習(xí)Java反射中java.beans包,吧知識(shí)點(diǎn)做了總結(jié),并把相關(guān)內(nèi)容做了關(guān)聯(lián),對(duì)此有需要的朋友可以學(xué)習(xí)參考下。2018-02-02Spring創(chuàng)建bean的幾種方式及使用場(chǎng)景
本文主要介紹了Spring創(chuàng)建bean的幾種方式及使用場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04maven倉(cāng)庫(kù)中心mirrors配置多個(gè)下載中心(執(zhí)行最快的鏡像)
這篇文章主要介紹了maven倉(cāng)庫(kù)中心mirrors配置多個(gè)下載中心(執(zhí)行最快的鏡像),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java中Timer的schedule()方法參數(shù)詳解
今天小編就為大家分享一篇關(guān)于Java中Timer的schedule()方法參數(shù)詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Java?swing創(chuàng)建一個(gè)窗口的簡(jiǎn)單步驟
這篇文章主要給大家介紹了關(guān)于Java?swing創(chuàng)建一個(gè)窗口的簡(jiǎn)單步驟,Java Swing是Java平臺(tái)下的GUI(Graphical User Interface,圖形用戶界面)工具包,提供了豐富的GUI組件,可以實(shí)現(xiàn)復(fù)雜的圖形界面應(yīng)用程序,需要的朋友可以參考下2024-06-06java面向?qū)ο笤O(shè)計(jì)原則之迪米特法則分析詳解
這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計(jì)原則之迪米特法則的示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,學(xué)有所得2021-10-10解決gateway報(bào)netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemor
這篇文章主要介紹了解決gateway報(bào)netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12