java数组模拟队列

利用一维数组 模拟队列的 添加数据 查看数据 获取数据 等基本功能



队列遵循 "先进先出"的原则

队列的作用就像电影院前的人们站成的排一样:第一个进入附属的人将最先到达队头买票。最后排队的人最后才能买到票。

class ArrayQueue {
    private int head;     //指向数组头部的前一个元素
    private int rear;     //指向数组的尾部
    private int maxSize;  //数组的最大容量
    private int[] arr;    //存放数据的数组

    public ArrayQueue(int maxsize) {
        maxSize = maxsize;
        head = -1;
        rear = -1;
        arr = new int[maxSize];
    }

    //判断队列是不是已经满了
    public boolean isFull() {
        if (rear+1 == maxSize) {
            return true;
        }
        return false;
    }

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

    //向队列中添加数据
    public void addQueue(int data) {
        if (isFull()) {
            System.out.println("队列数据已满,添加失败。");
            return;
        }
        rear++;
        arr[rear] = data;
        System.out.println("添加数据,成功");
    }

    //从队列中取出数据
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,取数据失败");
        }
        head++;
        return arr[head];
    }

    //显示队列中所有的数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空,显示数据失败");
            return;
        }
        for (int i=head+1;i<maxSize;i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
    //返回队列的头部数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("对了为空,显示失败");
        }
        return arr[head+1];
    }
}

可以用下面的主方法进行测试

import java.util.Scanner;
public class ArrayQueueDemo {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        ArrayQueue queue = new ArrayQueue(5);
        char c=' ';
        boolean loop=true;
        while(loop){
            System.out.println("s(show)表示显示所有数据");
            System.out.println("a(add)添加一个数据");
            System.out.println("g(get)获取头部数据");
            System.out.println("h(head)查看头部数据");
            System.out.println("e(exit)退出程序");
            c = scanner.next().charAt(0);
            switch (c){
                case 'a':
                    System.out.printf("请输入一个需要添加的数字:");
                    int i = scanner.nextInt();
                    queue.addQueue(i);
                    break;
                case 's':
                    queue.showQueue();
                    break;
                case 'g':
                    try {
                        int queue1 = queue.getQueue();
                        System.out.println(queue1);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int i1 = queue.headQueue();
                        System.out.println(i1);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop=false;
                    break;
                default:
                    System.out.println("输入信息有误,请重新输入");
            }
            System.out.println("-------------------");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值