622. 设计循环队列

Posted 爱吃榴莲的喵星人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了622. 设计循环队列相关的知识,希望对你有一定的参考价值。

文章目录


一、题目描述



题目链接


二、提供方便走读代码的图



三、题目代码

顺序表实现

typedef struct 
    int* a;
    int front;
    int tail;
    int k;
 MyCircularQueue;
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) 
    MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    cq->a = (int*)malloc(sizeof(int)*(k+1));
    cq->front = cq->tail = 0;
    cq->k = k;
    return cq;


bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
    if(myCircularQueueIsFull(obj))return false;
    obj->a[obj->tail] = value;
    obj->tail++;
    obj->tail %= (obj->k+1);
    return true;


bool myCircularQueueDeQueue(MyCircularQueue* obj) 
    if(myCircularQueueIsEmpty(obj))return false;
    obj->front++;
    obj->front %= (obj->k+1);
    return true;


int myCircularQueueFront(MyCircularQueue* obj) 
    if(myCircularQueueIsEmpty(obj))return -1;
    return obj->a[obj->front];


int myCircularQueueRear(MyCircularQueue* obj) 
    if(myCircularQueueIsEmpty(obj))return -1;
    if(obj->tail == 0)
    return obj->a[obj->k];
    else
    return obj->a[obj->tail-1];
    //int i = (obj->tail+obj->k)%(obj->k+1);
    //return obj->a[i];


bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
    return obj->front == obj->tail;


bool myCircularQueueIsFull(MyCircularQueue* obj) 
    return (obj->tail+1)%(obj->k+1) == obj->front;


void myCircularQueueFree(MyCircularQueue* obj) 
     free(obj->a);
     free(obj);


/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/

链表实现




typedef struct Queuenode
    struct Queuenode* next;
    int data;
 Queuenode;
typedef struct 
    struct Queuenode* head;
    struct Queuenode* tail;
 MyCircularQueue;
bool myCircularQueueIsFull(MyCircularQueue* obj);
bool myCircularQueueIsEmpty(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) 
    MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    Queuenode* newnode = (Queuenode*)malloc(sizeof(Queuenode));
    newnode->next=newnode;
    cq->head = cq->tail = newnode;
    for(int i=0 ; i < k; i++)
    
        Queuenode* knode = (Queuenode*)malloc(sizeof(Queuenode));
        cq->tail->next=knode;
        knode->next=cq->head;
        cq->tail=knode;
    
     cq->tail=cq->head;
     return cq;


bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
    if(myCircularQueueIsFull(obj))return false;
    obj->tail->data = value;
    obj->tail = obj->tail->next;
    return true;


bool myCircularQueueDeQueue(MyCircularQueue* obj) 
    if(myCircularQueueIsEmpty(obj))return false;
    obj->head = obj->head->next;
    return true;


int myCircularQueueFront(MyCircularQueue* obj) 
   if(myCircularQueueIsEmpty(obj))return -1;
   return obj->head->data;


int myCircularQueueRear(MyCircularQueue* obj) 
   if(myCircularQueueIsEmpty(obj))return -1;
   Queuenode* cur = obj->head;
   while(cur->next != obj->tail)
   
       cur = cur->next;
   
   return cur->data;


bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
    return obj->head == obj->tail;


bool myCircularQueueIsFull(MyCircularQueue* obj) 
    return obj->tail->next == obj->head;


void myCircularQueueFree(MyCircularQueue* obj) 
   Queuenode* cur = obj->head->next;
   Queuenode* head = obj->head;
   Queuenode* p = NULL;
   free(obj->head);
   while(cur != head)
   
       p = cur;
       cur = cur->next;
       free(p);
   
   free(obj);


/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/

以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。

以上是关于622. 设计循环队列的主要内容,如果未能解决你的问题,请参考以下文章

622. 设计循环队列

「LeetCode」622. 设计循环队列

「LeetCode」622. 设计循环队列

队列3:LeetCode622:设计循环队列

环形队列—LeetCode 622. 设计循环队列

设计循环队列(leetcode 622)