循环队列的代码具体实现

循环队列

1.循环队列的基本认识

在这里插入图片描述
在这里插入图片描述

2.具体实现

  • 用数组实现循环队列
2.1入队
  • 思路:
  1. 同样判断是否为满
  2. 添加的元素加在rear的位置,然后rear向后面移动,本来只需要加加,但是是循环队列,最后一个的下一个是0号下标,因此用模运算;
//入队
    public boolean enQueue(int value) {
        //1.先判断是否为满
        if(isFull()){
            return false;
        }
        this.elem[rear] = value;
        rear = (rear+1)%this.elem.length;
        return true;
    }

2.2出队
  • 思路:
  1. 首先判断是否为空
  2. 直接从队头front出;
public boolean deQueue() {
        //1.判断是否为空
        if(isEmpty()){
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }

2.3获取队头元素
  • 思路:因为是用数组实现,只要直接返回队头下标的元素就可以;
public int Front() {
        //1.判断是否为空
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }
2.4获取队尾元素
  • 思路:如果rear在0号位置返回上一个需要模运算;负责直接减减就可以;
//获取队尾
    public int Rear() {
        //1.判断是否为空
        if(isEmpty()){
            return -1;
        }
        int index = this.rear==0 ? this.elem.length-1 :  this.rear-1;
        return elem[index];
    }
2.5判断是否为空
  • 思路:只要rear == front就为空
//是否为空
    public boolean isEmpty() {
        if(rear == front){
            return true;
        }
        return false;
    }
2.6判断是否为满
  • 思路:只要判断rear的下一个是否为队头元素;
public boolean isFull() {
        if((rear+1)%elem.length == front){
            return true;
        }
        return false;
    }

3.完整代码

//循环队列(用数组实现)
class MyCircularQueue {
    public int[] elem;
    public int front;//队头
    public int rear;//队尾

    /** Initialize your data structure here. Set the size of the queue to be k. */
    //构造方法
    public MyCircularQueue(int k) {
        this.elem = new int[k+1];
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    //入队
    public boolean enQueue(int value) {
        //1.先判断是否为满
        if(isFull()){
            return false;
        }
        this.elem[rear] = value;
        rear = (rear+1)%this.elem.length;
        return true;
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    //出队
    public boolean deQueue() {
        //1.判断是否为空
        if(isEmpty()){
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }

    /** Get the front item from the queue. */
    //获取队头元素
    public int Front() {
        //1.判断是否为空
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }

    /** Get the last item from the queue. */
    //获取队尾
    public int Rear() {
        //1.判断是否为空
        if(isEmpty()){
            return -1;
        }
        int index = this.rear==0 ? this.elem.length-1 :  this.rear-1;
        return elem[index];
    }

    /** Checks whether the circular queue is empty or not. */
    //是否为空
    public boolean isEmpty() {
        if(rear == front){
            return true;
        }
        return false;
    }

    /** Checks whether the circular queue is full or not. */
    //是否为满
    public boolean isFull() {
        if((rear+1)%elem.length == front){
            return true;
        }
        return false;
    }
}

public class TestDemo4 {
    public static void main(String[] args) {
        MyCircularQueue myCircularQueue = new MyCircularQueue(8);
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(2);
        myCircularQueue.enQueue(3);
        myCircularQueue.enQueue(4);
        System.out.println(myCircularQueue.Front());
        System.out.println(myCircularQueue.Rear());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值