环形队列简单实现

环形队列就是将原本实现队列使用的数组或链表收尾相连,从而实现,在数组取出数据后头向后,空出位置可以继续存储数据

在这里插入图片描述

从队列中取出数据后 头向后 前面空出位置

在这里插入图片描述

简单实现

package com.test;

public class CircularQueue {
    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];
     }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单环形队列的C语言实现: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 5 int front = -1, rear = -1; int queue[MAX_SIZE]; int is_full() { return (rear + 1) % MAX_SIZE == front; } int is_empty() { return front == -1 && rear == -1; } void enqueue(int x) { if (is_full()) { printf("Queue is full\n"); return; } if (is_empty()) { front = rear = 0; } else { rear = (rear + 1) % MAX_SIZE; } queue[rear] = x; } void dequeue() { if (is_empty()) { printf("Queue is empty\n"); return; } if (front == rear) { front = rear = -1; } else { front = (front + 1) % MAX_SIZE; } } void display() { if (is_empty()) { printf("Queue is empty\n"); return; } printf("Queue: "); for (int i = front; i != rear; i = (i + 1) % MAX_SIZE) { printf("%d ", queue[i]); } printf("%d\n", queue[rear]); } int main() { enqueue(1); enqueue(2); enqueue(3); enqueue(4); enqueue(5); display(); dequeue(); dequeue(); dequeue(); display(); enqueue(6); enqueue(7); display(); return 0; } ``` 这个实现使用了一个数组来存储队列元素,使用了 front 和 rear 两个指针来指示队列的头和尾。当队列为空时,front 和 rear 均为 -1。当队列满时,rear 指向最后一个元素,即 (rear + 1) % MAX_SIZE == front。当队列不为空时,front 指向队列头,rear 指向队列尾。在入队时,如果队列已满则无法插入元素,否则将元素插入队列尾部。在出队时,如果队列为空则无法出队,否则将队列头部元素删除。在展示队列时,从 front 开始遍历到 rear,依次输出队列元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值