java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼內(nèi)容
ArrayQueue---用數(shù)組實(shí)現(xiàn)隊(duì)列
package com.structure; import java.util.Scanner; /** * @auther::9527 * @Description: 數(shù)組模擬隊(duì)列 * @program: jstl2 * @create: 2019-10-05 08:58 */ public class ArrayQueueDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //測(cè)試 ArrayQueue queue = new ArrayQueue(3); char key = ' '; //接受用戶輸入 boolean loop = true; //循環(huán)終止判斷器 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ù)"); System.out.println("按提示輸入......"); key = scanner.next().charAt(0); //接收數(shù)據(jù) switch (key) { case 's': queue.showQueue(); break; case 'a': System.out.println("請(qǐng)輸入一個(gè)數(shù)"); int value = scanner.nextInt(); queue.addQueue(value); break; case 'g': try { int res = queue.getQueue(); System.out.printf("取出的數(shù)是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = queue.headQueue(); System.out.printf("隊(duì)列頭部的數(shù)是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; default: System.out.println("...輸入不合法,請(qǐng)重新輸入..."); break; } } System.out.println("程序退出...."); } } //使用數(shù)組模擬隊(duì)列--編寫一個(gè)ArrayQueue class ArrayQueue { private int maxSize; //數(shù)組的最大容量 private int front; //隊(duì)列頭 private int rear; //隊(duì)列尾 private int[] arr; //存放數(shù)據(jù)的數(shù)組,模擬隊(duì)列. //創(chuàng)建隊(duì)列的構(gòu)造器 public ArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; front = -1; rear = -1; } //判斷隊(duì)列是否已經(jīng)滿了 public boolean isFull() { return rear == maxSize - 1; } //判斷隊(duì)列是否為空 public boolean isEmpty() { return rear == front; } //添加數(shù)據(jù)到隊(duì)列 public void addQueue(int n) { //判斷隊(duì)列是否已滿 if (isFull()) { System.out.println("隊(duì)列已滿,不能添加"); return; } rear++; //指針后移 arr[rear] = n; } //數(shù)據(jù)出隊(duì)列 public int getQueue() { //判斷隊(duì)列是否為空 if (isEmpty()) { throw new RuntimeException("隊(duì)列為空,不能取數(shù)據(jù)"); } front++; //頭部指針后移 return arr[front]; } //顯示隊(duì)列的所有數(shù)據(jù) public void showQueue() { if (isEmpty()) { System.out.println("隊(duì)列為空,沒(méi)有數(shù)據(jù)"); return; } for (int i = 0; i < arr.length; i++) { System.out.printf("arr[%d]=%d\n", i, arr[i]); } } //顯示隊(duì)列的頭部數(shù)據(jù),這個(gè)不是取出數(shù)據(jù) public int headQueue() { //判斷是否為空 if (isEmpty()) { System.out.println("隊(duì)列為空,沒(méi)有數(shù)據(jù)...."); throw new RuntimeException("隊(duì)列為空,沒(méi)有數(shù)據(jù)...."); } return arr[front + 1]; } }
改進(jìn)版---環(huán)形隊(duì)列:
環(huán)形隊(duì)列代碼
package com.structure; import java.util.Scanner; /** * @auther::9527 * @Description: 環(huán)形隊(duì)列 * @program: jstl2 * @create: 2019-10-05 09:53 */ public class CircleArrayQueueDemo { public static void main(String[] args) { //創(chuàng)建一個(gè)環(huán)形隊(duì)列 這里輸入最大數(shù)是4,其實(shí)有效的是3 CircleArray queue = new CircleArray(4); char key = ' '; // 接收用戶輸入 Scanner scanner = new Scanner(System.in);// boolean loop = true; // 輸出一個(gè)菜單 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);// 接收一個(gè)字符 switch (key) { case 's': queue.showQueue(); break; case 'a': System.out.println("輸出一個(gè)數(shù)"); int value = scanner.nextInt(); queue.addQueue(value); break; case 'g': // 取出數(shù)據(jù) try { int res = queue.getQueue(); System.out.printf("取出的數(shù)據(jù)是%d\n", res); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } break; case 'h': // 查看隊(duì)列頭的數(shù)據(jù) try { int res = queue.headQueue(); System.out.printf("隊(duì)列頭的數(shù)據(jù)是%d\n", res); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } break; case 'e': // 退出 scanner.close(); loop = false; break; default: break; } } System.out.println("程序退出~~"); } } class CircleArray { private int maxSize; //數(shù)組的最大容量 //front 變量調(diào)整:front就指向隊(duì)列的第一個(gè)元素,即:arr[front]=arr[0] private int front; //rear 變量調(diào)整:rear 初始值為0 指向隊(duì)列的最后一個(gè)元素的位置,因?yàn)樾枰粘鲆粋€(gè)位置作約束 private int rear; private int[] arr; //構(gòu)造器 public CircleArray(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } //判斷隊(duì)列是否已滿 public boolean isFull() { return (rear + 1) % maxSize == front; } //判斷隊(duì)列是否為空 public boolean isEmpty() { return rear == front; } //添加數(shù)據(jù)到隊(duì)列 public void addQueue(int n) { //判斷隊(duì)列是否已滿 if (isFull()) { System.out.println("隊(duì)列已滿,不能添加數(shù)據(jù)"); return; } //直接加入數(shù)據(jù) arr[rear] = n; //rear 后移,后移的時(shí)候,要通過(guò)取模來(lái)實(shí)現(xiàn)環(huán)形后移 rear = (rear + 1) % maxSize; } //獲取隊(duì)列的數(shù)據(jù),出隊(duì)列 public int getQueue() { if (isEmpty()) { throw new RuntimeException("隊(duì)列為空,不能取數(shù)據(jù)"); } //由于front是指向隊(duì)列的第一個(gè)元素,所以需要 // 1、front的值先保存到臨時(shí)變量 //2、將front后移一位,通過(guò)取模實(shí)現(xiàn)環(huán)形后移, // 3、將臨時(shí)變量返回 int temp = arr[front]; front = (front + 1) % maxSize; return temp; } //顯示隊(duì)列的所有數(shù)據(jù) public void showQueue() { if (isEmpty()) { System.out.println("隊(duì)列為空,沒(méi)有數(shù)據(jù)~"); return; } //遍歷環(huán)形隊(duì)列....這里有個(gè)小技巧,需要記住 for (int i = front; i < front + size(); i++) { System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]); } } //求出當(dāng)前隊(duì)列有效數(shù)據(jù)的個(gè)數(shù) public int size() { return (rear + maxSize - front) % maxSize; } //顯示隊(duì)列的頭部數(shù)據(jù) public int headQueue(){ //判斷是否為空 if (isEmpty()){ throw new RuntimeException("隊(duì)列為空,沒(méi)有數(shù)據(jù)"); } return arr[front]; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解
這篇文章主要介紹了 Java 向上轉(zhuǎn)型和向下轉(zhuǎn)型的詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目(圖文教程)
這篇文章主要介紹了新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目的圖文教程,需要的朋友可以參考下2018-01-01SpringBoot 嵌入式web容器的啟動(dòng)原理詳解
這篇文章主要介紹了SpringBoot 嵌入式web容器的啟動(dòng)原理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-11-11IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法
這篇文章主要介紹了IDEA 設(shè)置顯示內(nèi)存的使用情況和內(nèi)存回收的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04解決java main函數(shù)中的args數(shù)組傳值問(wèn)題
這篇文章主要介紹了解決java main函數(shù)中的args數(shù)組傳值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Java基于分治法實(shí)現(xiàn)的快速排序算法示例
這篇文章主要介紹了Java基于分治法實(shí)現(xiàn)的快速排序算法,結(jié)合實(shí)例形式分析了java基于分治法的快速排序相關(guān)實(shí)現(xiàn)技巧,代碼中備有較為詳細(xì)的注釋說(shuō)明便于理解,需要的朋友可以參考下2017-12-12mybatis-plus(insertBatchSomeColumn批量添加方式)
這篇文章主要介紹了mybatis-plus(insertBatchSomeColumn批量添加方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03