JAVA中数组实现循环队列

代码可以实现以下功能
s(show),显示队列
e(exit),退出程序
a(add),添加数据
g(get),取出数据
h(head),队首元素

代码实现


//测试主函数
public class 循环队列 {
    public static void main(String[] args) {
        System.out.println("测试环形队列 ");
        //创建一个环形队列
        circleArray queue = new circleArray(4);
        char key = ' ';   //接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单选项
        while (loop) {
            System.out.println("请选择要进行的操作");
            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 = scanner.next().charAt(0);
            switch (key) {
                case 's':
//                    try{
//                        queue.showQueue();
//                    }catch (Exception e){
//                        e.getMessage();
//                    }
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入要添加的元素");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.println("取出的元素是:" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.println("队列首元素是:" + res);
                    } catch (Exception e) {
                        e.getMessage();
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

//循环队列类
public class circleArray {
    private int maxSize;  //表示数组最大容量
    private int front;    //指向队列的第一个元素,初始值为0
    private int rear;     //指向对位的最后一个元素的后一个位置,初始值为0
    private int[] arr;
    public circleArray(int arrmaxSize) {
        maxSize = arrmaxSize;
        arr = new int[maxSize];
    }
    //判断队列空满
    public boolean isFull(){
        return (rear+1)%maxSize==front;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear==front;
    }
    //添加元素
    public void addQueue(int n){
        //判断是否已满
        if(isFull()){
            System.out.println("!!!!!!!!!!!队列已满!!!!!!!!!!!!!");
        }
        arr[rear]=n;
        //必须考虑取模
        rear = (rear+1)%maxSize;
    }
    //获取队列数据
    public int getQueue(){
        //判断是否为空
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        //front是队列的第一个元素
        //创建临时变量,保存front的值,front后移,考虑取模
        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }
    //显示队列所有数据
    public void showQueue(){
        //判断是否为空
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for(int i= front; i < front+size(); i++){
            System.out.println("arr["+i%maxSize+"] = "+arr[i%maxSize]);
        }
    }
    //获取队列有效元素
    public int size(){
        return (rear + maxSize - front)%maxSize;
    }
    //显示头元素
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值