数组模拟循环队列

import java.util.Scanner;

/**
 * @className  CircleArrayQueueDemo
 * @author  JopenChen 
 * @date  2020-09-08 12:48
 * @description 循环数组队列
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);

        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        while (loop) {
            System.out.println("a(add),添加元素");
            System.out.println("p(peek),获取头元素");
            System.out.println("g(get),取出元素");
            System.out.println("s(show),打印所有元素");
            System.out.println("e(exit),退出程序");
            char operation = scanner.next().charAt(0);
            switch (operation) {
                case 'a' :
                    System.out.println("请输入一个值:");
                    int value = scanner.nextInt();
                    try {
                        circleArrayQueue.add(value);
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    break;
                case 'p' :
                    try {
                        circleArrayQueue.peek();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    break;
                case 'g' :
                    try {
                        circleArrayQueue.get();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    break;
                case 's' :
                    try {
                        circleArrayQueue.show();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e' :
                    loop = false;
                    System.out.println("程序退出");
                    break;
                default:
                    break;
            }
        }

    }
}

class CircleArrayQueue{
    /**队列头*/
    private int front;
    /**队列尾*/
    private int rear;
    /**队列最大容量*/
    private int maxSize;
    /**实现队列的数组*/
    private int[] array;

    /**
     * 初始化队列
     */
    public CircleArrayQueue(int maxSize){
        this.maxSize = maxSize;
        array = new int[this.maxSize];
    }

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

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

    /**
     * 查看队列
     */
    public void show() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front + elementSum(); i++) {
            System.out.printf("array[%d] = %d\n", i % maxSize, array[i % maxSize]) ;
        }
    }

    /**
     * 有效元素数
     */
    public int elementSum() {
       return (rear + maxSize - front) % maxSize;
    }

    /**
     * 添加元素
     */
    public void add(int element) {
        if (isFull()) {
            throw new RuntimeException("队列·为满,不可插入");
        }
        array[rear] = element;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 取出元素
     */
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不可取出");
        }
        int temp = array[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    /**
     * 取出头元素
     */
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法读取头元素");
        }
        System.out.printf("array[%d] = %d", front, array[front]);
        return array[front];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值