数组模拟队列

package com.yg.queue;/*
@author  Ge
@date    2020/2/18  16:25
*/

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue queue = new CircleArrayQueue(3);
        char key=' ';
        Scanner sc=new Scanner(System.in);
        boolean loop=true;
        while (loop){
            System.out.println("s: 显示队列");
            System.out.println("e: 退出程序");
            System.out.println("a: 添加数据到队列");
            System.out.println("g: 从队列取出数据");
            System.out.println("h: 查看队列头的数据");
            key=sc.next().charAt(0);//接受一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'e':
                    sc.close();
                    loop=false;
                    break;
                case 'a':
                    System.out.println("请输入要添加的数字:");
                    int value=sc.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    System.out.println(queue.getQueue());
                    break;
                case 'h':
                    System.out.println(queue.getHeadData());
                    break;
                }

            }
        }


    }

     class CircleArrayQueue {
        private int front = 0;//指向当前队列的第一个元素
        private int rear = 0;//指向当前队列最后一个元素的后一个位置
        private int maxSize;//表示数组的最大容量
        private int[] arr;//用于存放数据模拟队列

        public CircleArrayQueue(int maxSize) {
            this.maxSize = maxSize;
            arr=new int[maxSize];
        }

        //判断队列是否为满
        public boolean isFull() {
            return (rear + 1) % maxSize == front;
        }

        //判断队列是否为空
        public boolean isEmpty() {
            return front == rear;
        }

        //添加数据到队列
        public void addQueue(int n) {
            if (isFull()) {
                throw new RuntimeException("队列已满,不能加元素");
            }
            arr[rear] = n;
            rear = (rear + 1) % maxSize;
        }

        //获取队列的数据
        public int getQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空,没有数据");
            }
            int value=arr[front];
            front=(front+1)%maxSize;
            return value;
        }

        //显示队列的所有数据
        public void showQueue(){
            if(isEmpty()){
                System.out.println("队列为空,没有数据");
                return;
            }
            for (int i = front; i <front+getSize() ; i++) {
                System.out.println("a["+i%maxSize+"]:"+arr[i%maxSize]);
            }
        }

        //查看队列头的数据
         public int getHeadData(){
            if(isEmpty()){
                System.out.println("队列为空");
                return 0;
            }
            return arr[front];
         }


        //获取队列有效数据个数
        public int getSize(){
            return (rear-front+maxSize)%maxSize;
        }
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值