设计循环队列

本文介绍了一种使用数组实现的循环队列数据结构。包括初始化、判断空与满、入队、出队、获取队首与队尾元素等操作,并提供了完整的C语言实现代码。

1、定义数据结构  tail head 头尾指针,队列buf queue ,队列长度 size   ,已存数据长度 len 

typedef struct myQueue{

    int* queue;

    int head;

    int tail;

    int size;

    int len;

} MyCircularQueue;

2.初始化 数据结构

/** Initialize your data structure here. Set the size of the queue to be k. */

 

MyCircularQueue* myCircularQueueCreate(int k) {

    MyCircularQueue* obj=malloc(sizeof(MyCircularQueue));

    obj->queue=malloc(k*sizeof(int));

    obj->head=0;

    obj->tail=0;

    obj->size=k;

    obj->len = 0;

    return obj;

}

3.如果已存在数据长度为0 则为空

/** Checks whether the circular queue is empty or not. */

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {

    return (obj->len==0);//&&((obj->queue)[obj->head]==-1);

}

4、如果已存在数据长度等于queue的长度则队列为满

/** Checks whether the circular queue is full or not. */

bool myCircularQueueIsFull(MyCircularQueue* obj) {

    return (obj->len==obj->size);

}

5、向尾指针插入数据,尾指针总是指向当前有效数据的下个地址,已存在数据长度加1

/** Insert an element into the circular queue. Return true if the operation is successful. */

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {

    if(myCircularQueueIsFull(obj)) return false;

    (obj->queue)[obj->tail]=value;

    obj->tail = (obj->tail + 1 )% obj->size;  

    obj->len ++;

    return true;

}

6、从头指针开始删除队列中元素,已存在数据长度减1,头指针加一

/** Delete an element from the circular queue. Return true if the operation is successful. */

bool myCircularQueueDeQueue(MyCircularQueue* obj) {

    if(myCircularQueueIsEmpty(obj)) return false;

       obj->head = (obj->head + 1) % obj->size;

       obj->len --;

    return true;

}

7、获取有效数据第一个元素

/** Get the front item from the queue. */

int myCircularQueueFront(MyCircularQueue* obj) {

     if(myCircularQueueIsEmpty(obj)) return -1;

    return (obj->queue)[obj->head];

}

8、获取有效数据最后一个元素

/** Get the last item from the queue. */

int myCircularQueueRear(MyCircularQueue* obj) {

    if(myCircularQueueIsEmpty(obj)) return -1; 

    if(obj->tail == 0)

    return (obj->queue)[obj->size - 1];

    else

     return (obj->queue)[obj->tail - 1];

}

9、释放queue

void myCircularQueueFree(MyCircularQueue* obj) {

    if(obj&&obj->queue)

    {

       free(obj->queue);

       free(obj);

    }  

}

在 Java 中设计循环队列可以通过数组来实现。循环队列是一种线性数据结构,它的操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。以下是一个使用 Java 实现循环队列的示例代码: ```java class MyCircularQueue { private int[] queue; private int front; private int rear; private int size; // 初始化队列 public MyCircularQueue(int k) { queue = new int[k]; front = 0; rear = -1; size = 0; } // 入队操作 public boolean enQueue(int value) { if (isFull()) { return false; } rear = (rear + 1) % queue.length; queue[rear] = value; size++; return true; } // 出队操作 public boolean deQueue() { if (isEmpty()) { return false; } front = (front + 1) % queue.length; size--; return true; } // 获取队首元素 public int Front() { if (isEmpty()) { return -1; } return queue[front]; } // 获取队尾元素 public int Rear() { if (isEmpty()) { return -1; } return queue[rear]; } // 判断队列是否为空 public boolean isEmpty() { return size == 0; } // 判断队列是否已满 public boolean isFull() { return size == queue.length; } } ``` 可以使用以下方式测试这个循环队列: ```java public class Main { public static void main(String[] args) { MyCircularQueue circularQueue = new MyCircularQueue(3); System.out.println(circularQueue.enQueue(1)); // 返回 true System.out.println(circularQueue.enQueue(2)); // 返回 true System.out.println(circularQueue.enQueue(3)); // 返回 true System.out.println(circularQueue.enQueue(4)); // 返回 false,队列已满 System.out.println(circularQueue.Rear()); // 返回 3 System.out.println(circularQueue.isFull()); // 返回 true System.out.println(circularQueue.deQueue()); // 返回 true System.out.println(circularQueue.enQueue(4)); // 返回 true System.out.println(circularQueue.Rear()); // 返回 4 } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值