欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java數(shù)組實現(xiàn)循環(huán)隊列示例介紹

 更新時間:2022年01月10日 09:54:41   作者:小輝小輝-  
大家好,本篇文章主要講的是java數(shù)組實現(xiàn)循環(huán)隊列示例介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

 從頂部進(jìn)去數(shù)據(jù),從底部出來數(shù)據(jù),用數(shù)組實現(xiàn)隊列,但是下面這個隊列,只能進(jìn)行一次存數(shù)值,取數(shù)值,不夠完善。

import java.util.Scanner;
 
public class ArrayQueueDemo {
    public static  void main(String[]args){
        //定義隊列大小maxsize
        ArrayQueue arrayQueue=new ArrayQueue(3);
        Scanner scanner=new Scanner(System.in);
        char key=' ';
        boolean  leap=true;
        while(leap){
            System.out.println("s(show):顯示隊列");
            System.out.println("e(exit):退出程序");
            System.out.println("h(head):顯示頭條數(shù)據(jù)");
            System.out.println("a(add):添加數(shù)據(jù)");
            System.out.println("g(get):從程序中取出隊列");
            System.out.println("請輸入一個數(shù)");
 
            key= scanner.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
 
                case 'h':
                    System.out.printf("頭數(shù)據(jù)為%d",arrayQueue.showHead());
                    break;
                case 'a':
                    System.out.println("輸入添加數(shù)據(jù)");
                   int data= scanner.nextInt();
                    arrayQueue.addQueue(data);
                    break;
                case 'g':
                   arrayQueue.getQueue();
                    break;
                case 'e':
                    leap=false;
                     break;
                default:
                    break;
 
 
            }
        }
 
    }
 
}
class ArrayQueue{
    private  int maxsize;//隊列大小
    private int rear;
    private int front;
    private int [] arry;
    public ArrayQueue(int maxsize1){
        maxsize=maxsize1;
        arry=new int[maxsize];
        front =-1;
        rear=-1;
    }
    //判斷隊列是否滿
    public boolean isFull(){
        return rear==maxsize-1;
    }
    //增加數(shù)據(jù)
    public  void addQueue(int data){
        if (isFull()){
            System.out.println("隊列滿了");
        }
        else{
            arry[rear+1]=data;
            rear++;
 
 
        }
    }
    //顯示數(shù)據(jù)
    public  void showQueue(){
        for (int i = 0; i < arry.length; i++) {
            System.out.println();
            System.out.printf("%d",arry[i]);
 
        }
    }
    //是否空
    public boolean isEmpety(){
        return rear== front;
    }
    //顯示頭數(shù)據(jù)
    public int showHead() {
        if (isEmpety()) {
 
            throw new RuntimeException("隊列是空的");
        }
        return arry[front +1];
 
    }
    public int getQueue(){
        if (isEmpety()){
            throw new RuntimeException("隊列是空");
 
        }
        System.out.printf("去除一個隊列為%d",arry[++front]);
        arry[front]=0;
 
        return 0;
 
    }
 
 
 
 
}

 循環(huán)隊列,可以進(jìn)行數(shù)據(jù)插入和取出,是上個普通隊列的增強(qiáng)版。上個隊列只能使用一次的情況被解決了

import java.util.Scanner;
 
public class CircleArrayQueueDemo {
    public  static void main(String[] args){
        //定義隊列大小maxsize
        cirCleArray cirCleArray=new cirCleArray(3);//隊列大小是三,但是有效數(shù)據(jù)是兩個
        Scanner scanner=new Scanner(System.in);
        char key=' ';
        boolean  leap=true;
        while(leap){
            System.out.println("s(show):顯示隊列");
            System.out.println("e(exit):退出程序");
            System.out.println("h(head):顯示頭條數(shù)據(jù)");
            System.out.println("a(add):添加數(shù)據(jù)");
            System.out.println("g(get):從程序中取出隊列");
            System.out.println("請輸入一個數(shù)");
 
            key= scanner.next().charAt(0);
            switch (key){
                case 's':
                    cirCleArray.showCircleArray();
                    break;
 
                case 'h':
                    System.out.printf("頭數(shù)據(jù)為%d",cirCleArray.showHead());
                    break;
                case 'a':
 
                   cirCleArray.addQueue();
                    break;
                case 'g':
                    cirCleArray.getQueue();
                    break;
                case 'e':
                    leap=false;
                    break;
                default:
                    break;
 
 
            }
 
 
    }
 
    }
}
    class cirCleArray{
        private int maxsize;
        private int front;
        private int rear;
        private int [] arry;
        Scanner scanner=new Scanner(System.in);
        public cirCleArray(int maxsize1){
            this.maxsize=maxsize1;
          arry=new int[maxsize];
            front=0;
            rear=0;
        }
        //顯示隊列
        public void showCircleArray(){
            if (isEmpty()){
                System.out.println("隊列為空");
            }
            else{
                for (int i =front; i < front+size(); i++) {
                    System.out.printf("arry[%d]=%d\n",i % maxsize,arry[i % maxsize]);
                }
            }
 
        }
        public  int size(){
            return (rear+maxsize-front)%maxsize;
        }
        //隊列為空
        public boolean isEmpty(){
            return front==rear;
        }
        //隊列為滿
        public boolean isFull(){
            return (rear+1)%maxsize==front;
        }
        //添加數(shù)據(jù)
        public void addQueue(){
 
           if (isFull()){
               System.out.printf("隊列滿了");
           }
           else{
               System.out.printf("輸入添加的數(shù)據(jù)");
               int  data=scanner.nextInt();
               arry[rear]=data;
             if (rear<maxsize-1){
                 rear++;
             }
             else {
                 rear=(rear+1)%maxsize;
             }
 
           }
 
 
        }
        //取出數(shù)據(jù)
        public void getQueue(){
            if (isEmpty()){
                System.out.println("隊列是空的");
            }
            else{
                System.out.printf("取出數(shù)據(jù)%d",arry[front]);
//               arry[front]=0;
//                front++;
                front=(front+1)%maxsize;
 
            }
        }
        //顯示頭數(shù)據(jù)
        public int showHead() {
            if (isEmpty()) {
 
                throw new RuntimeException("隊列是空的");
            }
            return arry[front];
 
        }
    }

到此這篇關(guān)于java數(shù)組實現(xiàn)循環(huán)隊列示例介紹的文章就介紹到這了,更多相關(guān)java數(shù)組循環(huán)隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解java 對象鎖與類鎖

    詳解java 對象鎖與類鎖

    這篇文章主要介紹了java 對象鎖與類鎖的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-09-09
  • IDEA打包的兩種方式及注意事項說明

    IDEA打包的兩種方式及注意事項說明

    這篇文章主要介紹了IDEA打包的兩種方式及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Mybatis聯(lián)合查詢的實現(xiàn)方法

    Mybatis聯(lián)合查詢的實現(xiàn)方法

    本文主要介紹了 Mybatis聯(lián)合查詢的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • intellij idea 啟動tomcat 1099端口被占用的解決

    intellij idea 啟動tomcat 1099端口被占用的解決

    這篇文章主要介紹了intellij idea 啟動tomcat 1099端口被占用的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java實現(xiàn)人機(jī)對戰(zhàn)猜拳游戲

    Java實現(xiàn)人機(jī)對戰(zhàn)猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)人機(jī)對戰(zhàn)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 將一個數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法

    將一個數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法

    下面小編就為大家?guī)硪黄獙⒁粋€數(shù)組按照固定大小進(jìn)行拆分成數(shù)組的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Java8?函數(shù)式編程stream流使用詳解

    Java8?函數(shù)式編程stream流使用詳解

    這篇文章主要介紹了Java8?函數(shù)式編程stream流使用詳解的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 基于紅黑樹插入操作原理及java實現(xiàn)方法(分享)

    基于紅黑樹插入操作原理及java實現(xiàn)方法(分享)

    下面小編就為大家分享一篇基于紅黑樹插入操作原理及java實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Redis打開rdb文件常用方法詳解

    Redis打開rdb文件常用方法詳解

    這篇文章主要介紹了Redis打開rdb文件常用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • IDEA打開項目所有東西都在報紅報錯的解決方案

    IDEA打開項目所有東西都在報紅報錯的解決方案

    這篇文章主要給大家介紹了關(guān)于IDEA打開項目所有東西都在報紅報錯的三個解決方案,文中通過圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用idea具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-06-06

最新評論