使用数组实现队列

本文介绍了队列的先入先出原则,并展示了如何通过数组实现队列,特别提到了循环数组的应用,以解决一次性使用的问题。

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

队列是一种有序列表,可以实现数据或链表来实现,队列遵循先入先出的原则,先存入的数据先取出,后存入的数据后取出。

队列的图形:maxSize代表数据的最大容量,front代表数组头部,rear代表数组的尾部。

1、下面直接通过代码来实现:

public static void main(String[] args) {
        //操作队列数据
        ArrayQueue queue = new ArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        char ch = ' ';
        boolean loop = true;
        while(true){
            System.out.println("请输入对应的字母操作程序");
            System.out.println("a:添加数据到队列");
            System.out.println("b:取出队列的数据");
            System.out.println("c:显示队列的所有数据");
            System.out.println("d:获取头部的数据");
            System.out.println("e:显示尾部数据");
            System.out.println("f:退出程序");
            //接收一个数据
            ch = scanner.next().charAt(0);
            switch (ch) {
            case 'a':
                System.out.println("请输入一个数:");
                int num = scanner.nextInt();
                queue.addQueue(num);
                break;
            case 'b':
                try {
                    int c = queue.getQueue();
                    System.out.println(c);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'c':
                try {
                    queue.showQueue();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'd':
                try {
                    int a = queue.headQueue();
                    System.out.println(a);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'e':
                try {
                    int b = queue.rearQueue();
                    System.out.println(b);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'f':
                loop = false;
                break;
            default:
                break;
            }
        }
    }
}

/**
 * 创建队列的类
 * @author Administrator
 *
 */
class ArrayQueue{
    private int maxSize;//队列的容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存储队列数据的数组
    
    /**
     * 添加队列的构造器
     */
    public ArrayQueue(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;//队列的头部
        rear = 0;//队列尾部
    }
    
    //判断队列是否已满
    public boolean isFull(){
        
        return maxSize == front;
    }
    
    //判断队列是否为空
    public boolean isEmpty(){
        
        return front == rear;
    }
    
    //添加数据到队列
    public void addQueue(int num){
        //先判断数据是否已满
        if(isFull()){
            System.out.println("队列数据已满,咱不能添加数据~~");
            return;
        }
        arr[rear] = num;

        rear++;
    }
    
    //获取队列的数据a
    public int getQueue(){
        //先判断数据是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,不能取数据~~");
        }
        int num = arr[front];
        front++;
        return num;
    }
    
    //显示队列的所有数据
    public void showQueue(){
        //先判断数据是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,不能取数据~~");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
    
    //获取头部的数据
    public int headQueue(){
        //先判断数据是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,不能取数据~~");
        }
        int b = arr[front];
        return b;
    }
    
    //获取尾部的数据
    public int rearQueue(){
        //先判断数据是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,不能取数据~~");
        }
        int a = arr[rear-1];
        return a;
    }    

注:这种方法实现的队列不能复用,只能使用一次,下面使用循环数组来实现。

public static void main(String[] args) {
        //开始操作环形数据
        AnnularQueue queue = new AnnularQueue(4);
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while(loop){
            System.out.println("请输入对应的字母开始操作程序:");
            System.out.println("a: 添加元素");
            System.out.println("b: 取出元素");
            System.out.println("c: 取出队列的头部元素");
            System.out.println("d: 取出队列的尾部元素");
            System.out.println("e: 遍历所有的元素");
            System.out.println("f: 退出程序");
            char ch = scanner.next().charAt(0);
            switch (ch) {
            case 'a':
                    System.out.println("请输入一个数据:");
                    int num = scanner.nextInt();
                    queue.addQueue(num);
                    break;
            case 'b':
                try {
                     int b = queue.getQueue();
                    System.out.println(b);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                
                break;
            case 'c':
                try {
                     int c = queue.headQueue();
                     System.out.println(c);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'd':
                try {
                    int d = queue.tailQueue();
                    System.out.println(d);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'e':
                try {
                    queue.showQueue();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                break;
            case 'f':
                scanner.close();
                loop = false;
                break;
            default:
                break;
            }
        }
    }
}


class AnnularQueue{
    private int maxSize;//队列的容量
    private int rear;//队列尾
    private int front;//队列头
    private int[] array;//存放队列的数据
    
    /**
     * 增加队列的构造方法
     */
    public AnnularQueue(int maxSize){
        this.maxSize = maxSize;
        array = new int[maxSize];
        rear = 0;//队列头默认从0开始,
        front = 0;
    }
    
    
    /**
     * 判断队列是否已满
     */
    public boolean isFull(){
        
        return (rear + 1) % maxSize == front;
    }
    
    /**
     * 判断队列是否为空
     */
    public boolean isEmpty(){
        
        return rear == front;
    }
    
    /**
     * 添加元素
     */
    public void addQueue(int num){
        //判断队列是否已满
        if(isFull()){
            System.out.println("队列已满,暂不能添加元素~~");
            return;
        }
        array[rear] = num;
        //rear需要自增,这里需要考虑的队列环形问题,需要取模操作
        rear = (rear + 1) % maxSize;
    }
    
    
    /**
     * 获取元素
     */
    public int getQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,无法取数据~~");
        }
        int num = array[front];
        //对front自增取模操作
        front = (front + 1) % maxSize;
        return num;
    }
    
    /**
     * 遍历元素
     */
    public void showQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,无法取数据~~");
        }
        for (int i = front; i < front + getSize(); i++) {
            System.out.printf("arr[%d] = %d\n",i % maxSize ,array[i % maxSize]);
        }
    }
    
    /**
     * 查询有效的元素
     */
    public int getSize(){
        
        return (rear + maxSize - front) % maxSize;
    }
    
    
    /**
     * 取出头部元素
     */
    public int headQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,无法取数据~~");
        }
        return array[front];
    }
    
    /**
     * 取出尾部元素
     */
    public int tailQueue(){
        //判断队列是否为空
        if(isEmpty()){
            throw new RuntimeException("数据为空,无法取数据~~");
        }
        if(rear==0){
            rear = 4;
        }
        return array[rear-1];
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值