数组模拟队列

这篇博客介绍如何使用环形数组来模拟队列,解决数组作为队列时只能一次性使用的限制。通过创建Sequence类,实现了添加、获取、打印和查看队列第一个元素的功能,并在主函数中提供交互式操作。示例代码展示了环形数组队列在实际应用中的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 学习数据结构与算法,看完视频,做的笔记
    使用数组模拟队列只能使用一次,队列添加达到最大后,全部取出数据就无法再添加了,所以需要使用环形数组队列
public class TestIndex {

    public static void main(String[] args) {
        System.out.println("系统初始化中");
        Sequence sequence = new Sequence(4);
        boolean flag  =  true;
        Scanner scanner = null;
        while (flag){
            scanner = new Scanner(System.in);
            System.out.println("【a】添加数据");
            System.out.println("【g】取出数据");
            System.out.println("【p】打印数据");
            System.out.println("【h】查看第一个数据");
            System.out.println("【e】系统退出");
            String i = scanner.next();
            try {
                switch (i){
                    case "a":
                        System.out.println("请输入一个数");
                        int anInt = scanner.nextInt();
                        sequence.add(anInt);
                        break;
                    case "g":
                        System.out.println(sequence.get());
                        break;
                    case "p":
                        sequence.print();
                        break;
                    case "h":
                        System.out.println(sequence.head());
                        break;
                    case "e":
                        scanner.close();
                        System.out.println("系统退出");
                        flag=false;
                        break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

}


class Sequence{
    private int front;
    private int last;
    private int [] arr;
    private int maxSize;

    public Sequence(int maxSize){
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        this.front = -1; // 头索引
        this.last = -1; // 尾索引
    }

    public boolean isEmpty(){
        if (front == last){
            return true;
        }else {
            return false;
        }
    }

    public boolean isFull(){
        if (front ==maxSize-1){
            return true;
        }else {
            return false;
        }
    }

    public void add(int num){
        if (isFull()){
            throw new RuntimeException("数组已满");
        }else {
            this.front++;
            this.arr[front] = num;
        }
    }

    public int get(){
        if (isEmpty()){
            throw new RuntimeException("数组为空,无法取数");
        }
        return arr[++last];
    }

    public int head(){
        if (isEmpty()){
            throw new RuntimeException("数组为空");
        }
        return arr[last+1];
    }

    public void print(){
        if (isEmpty()){
            throw new RuntimeException("数组为空");
        }
        System.out.println(Arrays.toString(arr));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值