数组模拟环形队列实现


一、环形队列实现思路

在这里插入图片描述

与数组实现的普通队列有所不同:
	最重要的一点是取模进行循环。
	1、front直接指向第一个有效元素,初始值为0
	2、rear指向最后一个有效元素的后一个位置,初始值为0
	3、队列为空:rear==fromt
	4、队列为满:(rear+1)%maxSize ==front
	5、有效元素的个数:(rear-front+maxSize)%maxSize

二、代码实现

package com.hao.demo;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Yzh
 * @Date: 2021/07/28/15:14
 * @Description:
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
        //进行测试
        CircleQueue circleQueue = new CircleQueue(4);
        Scanner scanner = new Scanner(System.in);
        char key ;//接收用户数据
        boolean loop = true;//是否循环
        while (loop){
            System.out.println("输入's',显示队列");
            System.out.println("输入'e',退出程序");
            System.out.println("输入'a',添加元素");
            System.out.println("输入'g',获取元素");
            System.out.println("输入'h',获取头元素");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    circleQueue.show();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    try{
                        System.out.println("请输入要添加的元素");
                        int data = scanner.nextInt();
                        circleQueue.add(data);
                    }catch(RuntimeException exception){
                        System.out.println(exception.getMessage());
                    }
                    break;
                case 'g':
                    try{
                        int res = circleQueue.get();
                        System.out.println("取出的元素是:"+res);
                    }catch(RuntimeException exception){
                        System.out.println(exception.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int head = circleQueue.getHead();
                        System.out.println("取出的头元素是: "+head);
                    }catch(RuntimeException exception){
                        System.out.println(exception.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
    }

}
class CircleQueue{
    //成员变量
    private int maxSize; //最大容量
    private int front;  //头指针,直接指向第一个元素,初始值为0
    private int rear;   //指向最后元素的后一个位置,初始值为0
    private int[] array;  //数组模拟队列
    /**
    * @Author: yzh
    * @Description: 有参构造器,用于创建CircleQueue实例
    * @Param: [maxSize]
    * @return:
    * @Date: 2021/7/28
    */
    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        array = new int[maxSize];
    }
    /**
     * @Author: yzh
     * @Description: 判断队列是否为满
     * @Param: []
     * @return: boolean
     * @Date: 2021/7/28
     */
    public boolean isFull(){
        //条件:(rear+1)%maxSize == front 为满
        return (rear+1)%maxSize == front;
    }
    /**
     * @Author: yzh
     * @Description: 判断队列是否已空
     * @Param: []
     * @return: boolean
     * @Date: 2021/7/28
     */
    public boolean isEmpty(){
        //条件:rear==front 为空
        return rear == front;
    }
    /**
     * @Author: yzh
     * @Description: 添加元素到队列
     * @Param: [data]
     * @return: void
     * @Date: 2021/7/28
     */
    public void add(int data){
        if(isFull()){
            throw new RuntimeException("队列已满,无法加入");
        }
        array[rear] = data;//本来就指向了最后一个元素的后一个位置,所以直接现在是rear++
        rear = (rear+1)%maxSize;
    }
    /**
     * @Author: yzh
     * @Description: 从队列中取出元素
     * @Param: []
     * @return: int
     * @Date: 2021/7/28
     */
    public int get(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法取出元素");
        }
        int data = array[front];
        front=(front+1)%maxSize;
        return data;
    }
    /**
     * @Author: yzh
     * @Description: 取出队列头元素
     * @Param: []
     * @return: int
     * @Date: 2021/7/28
     */
    public int getHead(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,无法取出元素");
        }
        return array[front];
    }
    /**
     * @Author: yzh
     * @Description: 显示队列中的所有元素,注意i的范围和写法
     * @Param: []
     * @return: void
     * @Date: 2021/7/28
     */
    public void show(){
        for (int i = front; i < front+size(); i++) {
            System.out.printf("Queue[%d] :[%d]\n",i%maxSize,array[i%maxSize]);
        }
    }
    /**
    * @Author: yzh
    * @Description: 返回队列的有效长度
    * @Param: []
    * @return: int
    * @Date: 2021/7/28
    */
    public int size(){
        return (rear-front+maxSize)%maxSize;
    }
}

总结

要了解清楚front和rear指向的位置。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值