這篇文章主要介紹了java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括黔西南州網(wǎng)站建設(shè)、黔西南州網(wǎng)站制作、黔西南州網(wǎng)頁制作以及黔西南州網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,黔西南州網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到黔西南州省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
代碼內(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); //測試 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("請輸入一個(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("...輸入不合法,請重新輸入..."); 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ì)列為空,沒有數(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ì)列為空,沒有數(shù)據(jù)...."); throw new RuntimeException("隊(duì)列為空,沒有數(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í)候,要通過取模來實(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后移一位,通過取模實(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ì)列為空,沒有數(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ì)列為空,沒有數(shù)據(jù)"); } return arr[front]; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。