普通队列和环形队列

该博客详细介绍了如何使用数组实现环形队列,包括队列的初始化、判断队列是否为空、是否已满、添加元素、获取元素、显示队列所有数据以及获取队列头元素的方法。此外,还提供了用户交互式的环形队列操作示例,展示了如何在Java中创建和操作环形队列。

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

普通

思路分析:
1)将尾指针往后移: rear+1, 当front== rear [ 空]

2)若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数
组元素中,否则无法存入数据。rear == maxSize- 1[队列满]

代码实现:

package com.main.dataStructures;
public class ArrayQueueDemo{
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(3);
        System.out.println(arrayQueue.headQueue());
        arrayQueue.allQueue();


    }
}
class ArrayQueue {
    private int maxSize;
    private int front;   //指向队列头,front是指向队列头的前一个位置
    private int rear;   //指向队列尾,rear是指向队列尾的位置,包括队列尾
    private int []arr;  //存放数据

    public ArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }
    //判空
    public boolean isEmpty(){
        return front == rear;
    }
    //判满
    public boolean isFull(){
        return rear == maxSize-1;
    }
    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            throw new RuntimeException("队列已满,无法添加");
        }
        rear++;
        arr[rear] = n;
    }
    //获取队列,出队列
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据");
        }
        front++;
        return arr[front];
    }
    //显示队列所有数据
    public void allQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for (int ar : arr) {
            System.out.println(ar);
        }
    }
    //显示队列头
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据");
        }
        return arr[front+1];
    }

}

环形

思路如下:
1.front变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说arr[front]就是队列的第一个元素
front的初始值= 0

2.rear 变量的含义做-一个调整: rear 指向队列的最后- -个元素的后一个位置.因为希望空出一- 个空间做为约定.
rear的初始值=0

3.当队列满时,条件是(rear +1) % maxSize==front [ 满]

4.对队列为空的条件,rear== front空

5.当我们这样分析,队列中有效的数据的个数(rear + maxSize- front) % maxSize //rear= 1 front=0

6.我们就可以在原来的队列.上修改得到,一个环形队列

代码实现:

package com.main.dataStructures;

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
        char key = ' ';//接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("a:添加");
            System.out.println("g:弹出");
            System.out.println("l:显示全部");
            System.out.println("h:头");
            System.out.println("e:退出");
            key = scanner.next().charAt(0);
            switch (key){
                case 'a':
                    System.out.println("输入一个数");
                    circleArrayQueue.addQueue(scanner.nextInt());
                    break;
                case 'g':
                    circleArrayQueue.getQueue();
                    break;
                case 'l':
                    circleArrayQueue.allQueue();
                    break;
                case 'h':
                    System.out.println(circleArrayQueue.headQueue());
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
                default:
                    break;
            }
        }
    }
}
class CircleArrayQueue{
    private int maxSize;
    private int front;   //front 就指向队列的第一个元素
    private int rear;   //rear 指向队列的最后一个元素的后一个位置.
    private int []arr;  //存放数据

    public CircleArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }
    //判空
    public boolean isEmpty(){
        return front == rear;
    }
    //判满
    public boolean isFull(){
        return (rear+1) % maxSize == front;
    }
    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            throw new RuntimeException("队列已满,无法添加");
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;
    }
    //获取队列,出队列
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据");
        }
        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }
    //显示队列所有数据
    public void allQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d",i % maxSize,arr[i%maxSize]);
            System.out.println();
        }
    }

    public int size(){
        return (rear-front+maxSize)%maxSize;
    }
    //显示队列头
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,无法获取数据");
        }
        return arr[front];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值