java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列的過程解析

本篇內(nèi)容介紹了“java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列的過程解析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比延平網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式延平網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋延平地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

代碼內(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("請(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è)ArrayQueueclass 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];  }}

“java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列的過程解析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

新聞標(biāo)題:java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列的過程解析
文章位置:http://www.muchs.cn/article26/ishijg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)公司、軟件開發(fā)、網(wǎng)站收錄、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管