线性数据结构-环形队列

在这里插入图片描述

public class Demo {
    public static void main(String[] args) {
        System.out.println("测试循环队列...");
        CircleArray circleArray = new CircleArray(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(showAll): 显示队列");
            System.out.println("e(exit): 退出");
            System.out.println("a(add): push入队");
            System.out.println("g(get): pop出队");
            System.out.println("h(head): 查看队头");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    circleArray.showAll();
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'a':
                    try {
                        System.out.println("请输入一个整数,用于入队:");
                        int value = scanner.nextInt();
                        circleArray.push(value);
                        System.out.println("入队成功!");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int value = circleArray.pop();
                        System.out.println("出队成功,元素为:"+value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int value = circleArray.showPeek();
                        System.out.println("队列头部元素为:"+value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("请输入正确的指令!");
            }
        }
        System.out.println("已退出!");
    }

}

class CircleArray {
    private int maxSize;//数组最大容量
    private int front;
    private int rear;
    private int[] queue;

    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        queue = new int[maxSize];
    }

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

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

    /**
     * 当前队列元素个数
     * @return
     */
    public int curSize() {
        return (rear-front+maxSize)%maxSize;
    }

    /**
     * 展示队列首部元素
     * @return
     */
    public int showPeek() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空...");
        }
        return queue[front];
    }

    /**
     * 入队
     * @param elm
     */
    public void push(int elm) {
        if (isFull()) {
            throw new RuntimeException("队列已满...");
        }
        queue[rear] = elm;
        rear = (rear+1)%maxSize;
    }

    /**
     * 出队
     */
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空...");
        }
        int popElem = queue[front];
        front = (front+1)%maxSize;
        return popElem;
    }

    /**
     * 展示队列中的全部元素
     */
    public void showAll() {
        for (int i = front; i < front+curSize(); i++) {
            System.out.printf("queue[%d]= %d\n",i, queue[i]);
        }
    }
}
测试循环队列...
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
a
请输入一个整数,用于入队:
1
入队成功!
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
a
请输入一个整数,用于入队:
2
入队成功!
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
s
queue[0]= 1
queue[1]= 2
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
h
队列头部元素为:1
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
g
出队成功,元素为:1
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
s
queue[1]= 2
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
a
请输入一个整数,用于入队:
111
入队成功!
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
a
请输入一个整数,用于入队:
111
队列已满...
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
s
queue[1]= 2
queue[2]= 111
s(showAll): 显示队列
e(exit): 退出
a(add): push入队
g(get): pop出队
h(head): 查看队头
e
已退出!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值