超强解析环形队列,简析单项,双向队列及基础功能实现---风之java

环形队列

超强解析环形队列(数组实现)

public class CircleQueueByArray {
    private int front;//指向队列首元素的前一个位置
    private int rear;//指向队列的最后一个元素
    private int[] arr;
    private int size;
    public CircleQueueByArray(int size){}//构造器初始化数组
    public boolean isFull(){}//判断队列是否满
    public boolean isEmpty(){}//判断队列是否空
    public void add(int data){}//入队
    public int delete(){}//出队
    public void ergodic(){}遍历环形队列

接下来一一解析各方法实现

构造方法

这里数组能放进的数据数量为size,但数组真实容量为size+1。所空出的一格用来判断数组是否为空。

    public CircleQueueByArray(int size){
        this.size=size;
        arr=new int[this.size+1];
        front=0;
        rear=0;
    }

判断队列是否满

在这里插入图片描述
这里以size=3为例,当其为3时,数组实际大小为4,由上图可易知
(rear+1)%(size+1)=0;而front为0,所以可以判断当(rear+1)%(size+1)==front时,队列为满。以后增加或删除元素是此条件仍成立。

    public boolean isFull(){
        return (rear+1)%(size+1)==front;
    }

判断队列是否为空

有上面的图片可易知,队列为初值时或将队列中的元素删减完时,rear==front。

    public boolean isEmpty(){
        return rear==front;
    }

入队

多次入队时环形队列需要rear从起始位置绕到终点位置在绕到起始位置,需要重复利用。故:rear=(rear+1)%(size+1);

    //入队
    public void add(int data){
        if(isFull()){
            System.out.println("队列已满,无法入队");
            return;
        }else{
            rear=(rear+1)%(size+1);
            arr[rear]=data;
        }
    }

出队

同rear一样,多次出队时环形队列需要front从起始位置绕到终点位置在绕到起始位置,需要重复利用。故:front=(front+1)%(size+1);

    public int delete(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法出列");
        }else{

            front=(front+1)%(size+1);
            return arr[front-1];
        }
    }

遍历环形队列

front指向队列首元素的前一个位置,所以cur=(front+1)%(size+1)是让cur指向首元素地址。在遍历过程中有可能出现front=3,rear=1类似于这样的情况。所以遍历是需要将cur%=size+1 以保证无论何时cur都能完整的遍历数组。

    public void ergodic(){
        if(isEmpty()){
            System.out.println("队列为空,无法遍历");
            return;
        }
        int cur=(front+1)%(size+1);
        while(cur!=rear){
            System.out.print(arr[cur]+" ");
            cur++;
            cur%=size+1;
        }
        System.out.print(arr[rear]);
        System.out.println();
    }

单项队列

数组实现单项队列

public class QueueByArray {
    private int front=-1;
    private int rear=-1;
    private int[] arr;
   public  int size;
    //构造函数中初始化数组容量
    public QueueByArray(int s){
        this.size=s;
        arr=new int[this.size];
    }
    //入队
    public void add(int data){
        if(rear==this.size-1){
            System.out.println("队列已满,无法添加数据");
            return;
        }else{
            arr[++rear]=data;
        }
    }
    //出队
    public int delete(){
        if(front>=rear){
            throw new RuntimeException("队列为空,无法添加数据");
        }else{
            int tmp=arr[front+1];
            for(int i=0;i<rear;i++)
                arr[i]=arr[i+1];
            rear--;
            return tmp;
            //return arr[++front];
        }
    }
    //遍历队列
    public void ergodic(){
        if(front>=rear){
            System.out.println("队列为空,无法添加数据");
            return;
        }
        int cur=front+1;
        while(true){
            System.out.println(arr[cur]);
            if(cur==rear)
                break;
            cur++;
        }
    }
}

链表实现单项队列

先创建一个节点类

public class Node {
    int data;
    Node next;
    public Node(){}
    public Node(int data){
        this.data=data;
        next=null;
    }
}

再创建一个用链表实现的队列类

public class QueueByLink {
    private Node front;
    private Node rear;
    public QueueByLink(){
        front=null;
        rear=null;
    }
    //入队
    public void add(int data){
        Node newNode=new Node(data);
        if(rear==null){
            front=newNode;
            rear=newNode;
        }else{
            rear.next=newNode;
            rear=newNode;
        }
    }
    //出列
    public Node delete(){
        if(rear==null) {
            throw new RuntimeException("队列为空,无法删除数据");
        }else if(rear!=front){
            Node cur=front.next;
            front=front.next;
            return cur;
        }else{
            front=null;
            rear=null;
            return null;
        }
    }
    //便利队列
    public void ergodic(){
        if(rear==null){
            System.out.println("队列为空,无法遍历");
            return;
        }
        Node cur=front;
        while(cur!=null){
            System.out.println(cur.data);
            cur=cur.next;
        }
    }
}

双向对列

链表实现双向队列

先创建一个节点类

public class Node {
    int data;
    Node next;
    public Node(){}
    public Node(int data){
        this.data=data;
        next=null;
    }
}

再创建一个用链表实现的队列类

public class Deque {
    private Node front;
    private Node rear;
    //判断队列是否为空
    public boolean isEmpty(){
        return rear==null;
    }
    //头入队
    public void fadd(int data){
        Node newNode=new Node(data);
        if(isEmpty()){
            front=newNode;
            rear=newNode;
        }else{
            newNode.next=front;
            front=newNode;
        }
    }
    //尾入队
    public void radd(int data){
        Node newNode=new Node(data);
        if(isEmpty()){
            front=newNode;
            rear=newNode;
        }else{
            rear.next=newNode;
            rear=newNode;
        }
    }
    //头出队
    public Node fdelete(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法删除");
        }else{
            Node first=front;
            front=front.next;
            return first;
        }
    }
    //尾出队
    public Node rdelete(){
        if(isEmpty()){
            throw new RuntimeException("队列为空,无法删除");
        }else{
            Node bef=rear;
            Node cur=front;
            while(cur.next!=rear)
                cur=cur.next;
            cur.next=rear.next;
            rear=cur;
            return bef;
        }
    }
    //遍历队列
    public void ergodic(){
        Node cur=front;
        while(cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}

  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
春天工作室是一个由孙老师负责的研究团队,他们研究的一个重要课题是NR Prach信道和随机介入流程。下面是对这个课题的简要分析。 NR Prach信道是指用于5G新无线通信技术中的物理随机接入信道。在5G网络中,由于用户设备的数量庞大,为了实现高效的随机接入过程,NR Prach信道被引入。它允许多个用户设备同时发送随机接入请求,而不会导致冲突。NR Prach信道的设计考虑了多路径传播和干扰等因素,以保证高质量的无线通信。 随机接入过程是指用户设备在接入网络时采取的一系列步骤。在5G网络中,随机接入过程包括以下几个关键步骤。首先,用户设备将在预定义的时间和频率资源上发送预定义的随机接入信号。然后,基站将收到来自多个用户设备的随机接入信号,并利用信道估计等技术来解调这些信号。接下来,基站通过分析解调后的信号来确定哪些用户设备成功接入网络。最后,基站将发送相应的消息给这些成功接入的用户设备,以确认它们已经接入网络。 在NR Prach信道和随机介入流程的研究中,春天工作室的孙老师可能会关注以下几个方面。首先,他们可能会研究如何优化NR Prach信道的设计,以提高无线通信的效率和可靠性。其次,他们可能会研究如何改进随机接入过程,以减少干扰和冲突,提高网络容量。此外,他们还可能探索新的技术和算法,以应对不同场景和应用对NR Prach信道和随机接入的需求。 总之,NR Prach信道和随机介入流程是5G无线通信中一个重要的研究课题。春天工作室的孙老师和他的团队将致力于对NR Prach信道和随机接入过程进行深入研究,以改进无线通信的性能和用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值