Java大数据平台开发 学习笔记(1)—— 数组模拟环形队列实现

一、数据结构与算法:


Step 1) 定义环形数组元素:

 private int maxSize;//环形数组长度
 private int front;	 //环形数组头指针	
 private int rear;	 //环形数组尾指针
 private int[] arr;	 //环形数组

Step 2) 初始化元素:

  public ArrayQueue(int arrMaxSize){
      maxSize = arrMaxSize;
      arr = new int[maxSize];
      front = 0;
      rear = 0;
  }

Step 3) 环形数组存满条件:

    public boolean inFull(){
        return front == (rear + 1) % maxSize;
    }
    

Step 4) 环形数组为空条件:

    public boolean isEmpty(){
        return rear == front;
    }
    

Step 5) 存入数据方法:

    public void addQueue(int n){
        if(inFull()){
            System.out.println("数据已经存满,无法存入");
            return;
        }
        System.out.println("数据存入成功");
        arr[rear++] = n;
        rear%=maxSize;
    }
    

Step 6) 取出数据方法:

    public void getQueu(){
        if(isEmpty()){
            System.out.println("数据为空,无法获取");
        }
        System.out.printf("取出的数据为:%d \r\n",arr[front]);
        arr[front++] = 0;
        front%=maxSize;
    }
    

Step 7) 遍历数据方法:

    public void showQueue(){
        if( isEmpty()){
            System.out.println("数据为空,无法打印");
            return;
        }
        for (int i=0 ; i<arr.length ; i++) {
            System.out.printf("arr[%d] = %d \r\n", i, arr[i]);
        }
    }
    

Step 8) 查看数据头方法:

    public void headQueue(){
        if( isEmpty()){
            System.out.println("数据为空,没有数据");
        }
        System.out.printf("取出的队列头为:%d \r\n",arr[front]);
    }
    

Step 9) main 方法:

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(4);
        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add ): 添加数据");
            System.out.println("g(get ): 取出数据");
            System.out.println("h(head): 查看头的数据");
            key = sc.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数据");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        arrayQueue.getQueu();
                    }catch(Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        arrayQueue.headQueue();
                    }catch(Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
    

• 由 ChiKong_Tam 写于 2020 年 9 月 4 日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值