环形队列的实现(什么是环形队列)

环形队列可以使用数组实现,也可以使用循环链表实现。packagewww.bittech;publicclassMyCircularQueue{privateintfront;//队列头privateintrear;//队列尾privateintusedSize;//数据个数privateint[]elem;//数组…

大家好,又见面了,我是你们的朋友全栈君。

环形队列可以使用数组实现,也可以使用循环链表实现。

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

package www.bittech;

public class MyCircularQueue {
    private int front;//队列头
    private int rear;//队列尾
    private int usedSize;//数据个数
    private int[] elem;//数组
    public MyCircularQueue(int k){
        this.elem=new int[k];
        this.front=0;
        this.rear=0;
        this.usedSize=0;
    }
    public boolean enQueue(int value){
        if(isFull()){
            return false;
        }
        this.elem[this.rear]=value;
        this.usedSize++;
        this.rear=(this.rear+1)%this.elem.length;
        return true;
    }
    //队尾下标加上1在%
    public boolean isFull(){
        if((this.rear+1)%this.elem.length==this.front){
            return true;
        }
        return false;
    }
    public boolean isEmpty(){
        return this.rear==this.front;
    }
    public boolean deQueue(int value){
        if(isEmpty()){
            return false;
        }
        this.elem[front]=value;
        this.front=(this.front+1)%this.elem.length;
        this.usedSize--;
        return true;
    }
    public int Front(){
        if(isEmpty()){
            throw new UnsupportedOperationException("队列为空");
        }
        return this.elem[this.front];
    }
     public int Rear(){
         if(isEmpty()){
             throw new UnsupportedOperationException("队列为空");
         }
         int index=this.rear == 0 ? this.elem.length-1 : this.rear-1;
         return this.elem[index];
     }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125500.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Fckeditor使用方法

    Fckeditor使用方法Fckeditor使用方法

    2022年4月25日
    41
  • MS17010(永恒之蓝)漏洞复现

    MS17010(永恒之蓝)漏洞复现MS17010(永恒之蓝)漏洞利用与复现首先需要准备win7的环境做为靶机获取信息利用nmap扫描得到,靶机IPnmap-sP192.168.229.0/24得到:IP:192.168.229.169然后利用:nmap–script=vuln192.168.229.169得到靶机信息:发现可能利用的漏洞:ms17-010然后启动msfmsfconsole然后查找漏洞路径:searchms17-010使用payload进行攻击:useexploit

    2022年4月30日
    54
  • busybox 安装mysql_busybox 安装问题解决「建议收藏」

    busybox 安装mysql_busybox 安装问题解决「建议收藏」直接编译错误1。loginutils/passwd.c:93:16:error:storagesizeof‘rlimit_fsize’isn’tknown解决方法:在busybox根目录下查找到文件:find-namelibbb.h在libbb.h中添加#include1。MakefileMakefile:431:***mixedimplicitandnormalr…

    2022年7月25日
    16
  • synchronousqueue场景_java中SynchronousQueue的核心方法

    synchronousqueue场景_java中SynchronousQueue的核心方法我们之前提过SynchronousQueue入队和出队的两种方法,其实它们都依托transfer方法得以实现。相比较而言,transfer可以同步进行入队和出队的操作,是SynchronousQueue中最重要的核心方法。下面我们就transfer概念、使用场景,以及在代码中增减元素的实例带来全面介绍。1.transfer概念进行匹配交换数据,SynchronousQueue内部使用Transfe…

    2022年6月22日
    49
  • Azure和.NET Core成就天作之合

    Azure和.NET Core成就天作之合

    2022年4月2日
    47
  • winform界面美化

    winform界面美化 介绍如下:1.这是DELPHI的换肤软件的DOTNET版,使用简单并有皮肤编辑工具,可到官方网站去下,当前版本为:1.20.1,更新时间:2008-04-202.这次发布的版本包括了DotNetSkin的ForVS2003和VS2005两个版本。3.未破解前,如果要显示标题栏,则标题标显示的是Logo图片,样式请看官方的DEMO程序;如果不显示标题栏,则在程序启动的时候提示一个对话框(T

    2022年5月8日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号