iT邦幫忙

0

【LeetCode with C: A Series of Problem-Solving Techniques】-- Design Circular Queue

  • 分享至 

  • xImage
  •  

Description

  1. Design Circular Queue

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Implement the MyCircularQueue class:

MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1.
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not.
boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.

Example 1:

Input
["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 3, true, true, true, 4]

Explanation
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
myCircularQueue.enQueue(1); // return True
myCircularQueue.enQueue(2); // return True
myCircularQueue.enQueue(3); // return True
myCircularQueue.enQueue(4); // return False
myCircularQueue.Rear(); // return 3
myCircularQueue.isFull(); // return True
myCircularQueue.deQueue(); // return True
myCircularQueue.enQueue(4); // return True
myCircularQueue.Rear(); // return 4

Constraints:

1 <= k <= 1000
0 <= value <= 1000
At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.

Answer & Explaining

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct { //定義環狀Queue結構
    int *data; //儲存元素的data
    int front; //指向front的指針
    int rear; //指向rear的指針
    int capacity; //最大空間
} MyCircularQueue;

MyCircularQueue* myCircularQueueCreate(int k) {
    //動態分配大小為k的queue
    MyCircularQueue *queue = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    queue->data = (int*)malloc(sizeof(int) * k);
    queue->front = -1; //初始化
    queue->rear = -1; //初始化
    queue->capacity = k; //容量設定為k
    return queue;
}

//檢查環狀queue是否滿
bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->rear + 1) % obj->capacity == obj->front;
}
//檢查環狀queue是否空
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front == -1;
}
//在環狀queue中插入元素
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj)) {
        return false;//滿則失敗
    }
    if (myCircularQueueIsEmpty(obj)) {
        obj->front = 0;//空則將obj設置為front指針
    }
    //其他的情況
    obj->rear = (obj->rear + 1) % obj->capacity;//移動rear指針
    obj->data[obj->rear] = value;//插入元素
    return true;
}
//在環狀queue中刪除元素
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    //環狀queue為空則回傳失敗
    if (myCircularQueueIsEmpty(obj)) {
        return false;
    }
    //環狀queue只有一個元素則重置front和rear指針
    if (obj->front == obj->rear) {
        obj->front = obj->rear = -1;
    } else { //其他情況則移動front
        obj->front = (obj->front + 1) % obj->capacity;
    }
    return true;
}
//獲取front元素
int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) { 
        return -1;//為空則回傳-1
    }
    return obj->data[obj->front];//否則回傳front
}
//獲取rear元素
int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return -1;//為空則回傳-1
    }
    return obj->data[obj->rear];//否則回傳rear
}
//釋放記憶體空間
void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->data);
    free(obj);
}

Testing

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct { //定義環狀Queue結構
    int *data; //儲存元素的data
    int front; //指向front的指針
    int rear; //指向rear的指針
    int capacity; //最大空間
} MyCircularQueue;

MyCircularQueue* myCircularQueueCreate(int k) {
    //動態分配大小為k的queue
    MyCircularQueue *queue = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    queue->data = (int*)malloc(sizeof(int) * k);
    queue->front = -1; //初始化
    queue->rear = -1; //初始化
    queue->capacity = k; //容量設定為k
    return queue;
}

//檢查環狀queue是否滿
bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->rear + 1) % obj->capacity == obj->front;
}
//檢查環狀queue是否空
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front == -1;
}
//在環狀queue中插入元素
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj)) {
        return false;//滿則失敗
    }
    if (myCircularQueueIsEmpty(obj)) {
        obj->front = 0;//空則將obj設置為front指針
    }
    //其他的情況
    obj->rear = (obj->rear + 1) % obj->capacity;//移動rear指針
    obj->data[obj->rear] = value;//插入元素
    return true;
}
//在環狀queue中刪除元素
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    //環狀queue為空則回傳失敗
    if (myCircularQueueIsEmpty(obj)) {
        return false;
    }
    //環狀queue只有一個元素則重置front和rear指針
    if (obj->front == obj->rear) {
        obj->front = obj->rear = -1;
    } else { //其他情況則移動front
        obj->front = (obj->front + 1) % obj->capacity;
    }
    return true;
}
//獲取front元素
int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) { 
        return -1;//為空則回傳-1
    }
    return obj->data[obj->front];//否則回傳front
}
//獲取rear元素
int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj)) {
        return -1;//為空則回傳-1
    }
    return obj->data[obj->rear];//否則回傳rear
}
//釋放記憶體空間
void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->data);
    free(obj);
}

int main() {
    MyCircularQueue* myCircularQueue = myCircularQueueCreate(3);
    printf("Enqueue 1: %s\n", myCircularQueueEnQueue(myCircularQueue, 1) ? "true" : "false"); // 插入 1
    printf("Enqueue 2: %s\n", myCircularQueueEnQueue(myCircularQueue, 2) ? "true" : "false"); // 插入 2
    printf("Enqueue 3: %s\n", myCircularQueueEnQueue(myCircularQueue, 3) ? "true" : "false"); // 插入 3
    printf("Enqueue 4: %s\n", myCircularQueueEnQueue(myCircularQueue, 4) ? "false" : "true"); // 插入 4,应返回 false(队列已满)
    printf("Rear: %d\n", myCircularQueueRear(myCircularQueue));  // 获取队列尾部元素,应返回 3
    printf("Is Full: %s\n", myCircularQueueIsFull(myCircularQueue) ? "true" : "false");  // 检查队列是否已满,应返回 true
    printf("Dequeue: %s\n", myCircularQueueDeQueue(myCircularQueue) ? "true" : "false"); // 删除一个元素
    printf("Enqueue 4: %s\n", myCircularQueueEnQueue(myCircularQueue, 4) ? "true" : "false"); // 插入 4
    printf("Rear: %d\n", myCircularQueueRear(myCircularQueue));  // 获取队列尾部元素,应返回 4
    myCircularQueueFree(myCircularQueue);  // 释放队列内存
    return 0;
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言