|
| 1 | +/* |
| 2 | + * Copyright (C) 2022, Saul Lawliet <october dot sunbathe at gmail dot com> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * front |
| 6 | + * | |
| 7 | + * [0, 1, 2] |
| 8 | + * | |
| 9 | + * rear |
| 10 | + * |
| 11 | + * 当队列为空的时候, front = -1, rear = -1; |
| 12 | + * 其他情况下, front 和 rear 均为对应的下标, 这样处理起来就会简单很多 |
| 13 | + */ |
| 14 | + |
| 15 | +#include <stdbool.h> |
| 16 | +#include "c/data-structures/array.h" |
| 17 | +#include "c/test.h" |
| 18 | + |
| 19 | +typedef struct { |
| 20 | + int *data; |
| 21 | + int size; |
| 22 | + int front; |
| 23 | + int rear; |
| 24 | +} MyCircularQueue; |
| 25 | + |
| 26 | +MyCircularQueue *myCircularQueueCreate(int k) { |
| 27 | + MyCircularQueue *obj = malloc(sizeof(MyCircularQueue)); |
| 28 | + obj->data = malloc(sizeof(int) * k); |
| 29 | + obj->size = k; |
| 30 | + obj->front = -1; |
| 31 | + obj->rear = -1; |
| 32 | + return obj; |
| 33 | +} |
| 34 | + |
| 35 | +bool myCircularQueueIsEmpty(MyCircularQueue *obj) { |
| 36 | + return obj->front == -1 && obj->rear == -1; |
| 37 | +} |
| 38 | + |
| 39 | +bool myCircularQueueIsFull(MyCircularQueue *obj) { |
| 40 | + return (obj->rear + 1) % obj->size == obj->front; |
| 41 | +} |
| 42 | + |
| 43 | +bool myCircularQueueEnQueue(MyCircularQueue *obj, int value) { |
| 44 | + if (myCircularQueueIsFull(obj)) return false; |
| 45 | + |
| 46 | + if (obj->front == -1) { |
| 47 | + obj->front = 0; |
| 48 | + obj->rear = 0; |
| 49 | + } else { |
| 50 | + obj->rear = (obj->rear + 1) % obj->size; |
| 51 | + } |
| 52 | + obj->data[obj->rear] = value; |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +bool myCircularQueueDeQueue(MyCircularQueue *obj) { |
| 57 | + if (myCircularQueueIsEmpty(obj)) return false; |
| 58 | + if (obj->front == obj->rear) { // 移除最后一个的时候, 重置索引 |
| 59 | + obj->front = -1; |
| 60 | + obj->rear = -1; |
| 61 | + } else { |
| 62 | + obj->front = (obj->front + 1) % obj->size; |
| 63 | + } |
| 64 | + return true; |
| 65 | +} |
| 66 | + |
| 67 | +int myCircularQueueFront(MyCircularQueue *obj) { |
| 68 | + if (myCircularQueueIsEmpty(obj)) return -1; |
| 69 | + return obj->data[obj->front]; |
| 70 | +} |
| 71 | + |
| 72 | +int myCircularQueueRear(MyCircularQueue *obj) { |
| 73 | + if (myCircularQueueIsEmpty(obj)) return -1; |
| 74 | + return obj->data[obj->rear]; |
| 75 | +} |
| 76 | + |
| 77 | +void myCircularQueueFree(MyCircularQueue *obj) { |
| 78 | + free(obj->data); |
| 79 | + free(obj); |
| 80 | +} |
| 81 | + |
| 82 | +void test(char *expectStr, char *commandStr, char *argStr) { |
| 83 | + arrayEntry *expectEntry = arrayParse1D(expectStr, ARRAY_STRING); |
| 84 | + arrayEntry *commandEntry = arrayParse1D(commandStr, ARRAY_STRING); |
| 85 | + arrayEntry *argEntry = arrayParse2D(argStr, ARRAY_INT); |
| 86 | + |
| 87 | + MyCircularQueue *obj; |
| 88 | + for (int i = 0; i < arraySize(commandEntry); i++) { |
| 89 | + char *expect = ((char **)arrayValue(expectEntry))[i]; |
| 90 | + char *command = ((char **)arrayValue(commandEntry))[i]; |
| 91 | + int *args = ((int **)arrayValue(argEntry))[i]; |
| 92 | + |
| 93 | + if (strcmp(command, "MyCircularQueue") == 0) { // Initializes the object with the size of the queue to be k. |
| 94 | + obj = myCircularQueueCreate(args[0]); |
| 95 | + } else if (strcmp(command, "Front") == 0) { // Gets the front item from the queue. If the queue is empty, return -1. |
| 96 | + EXPECT_EQ_INT(atoi(expect), myCircularQueueFront(obj)); |
| 97 | + } else if (strcmp(command, "Rear") == 0) { // Gets the last item from the queue. If the queue is empty, return -1. |
| 98 | + EXPECT_EQ_INT(atoi(expect), myCircularQueueRear(obj)); |
| 99 | + } else if (strcmp(command, "enQueue") == 0) { // Inserts an element into the circular queue. Return true if the operation is successful. |
| 100 | + EXPECT_EQ_INT(strcmp(expect, "true") == 0, myCircularQueueEnQueue(obj, args[0])); |
| 101 | + } else if (strcmp(command, "deQueue") == 0) { // Deletes an element from the circular queue. Return true if the operation is successful. |
| 102 | + EXPECT_EQ_INT(strcmp(expect, "true") == 0, myCircularQueueDeQueue(obj)); |
| 103 | + } else if (strcmp(command, "isEmpty") == 0) { // Checks whether the circular queue is empty or not. |
| 104 | + EXPECT_EQ_INT(strcmp(expect, "true") == 0, myCircularQueueIsEmpty(obj)); |
| 105 | + } else if (strcmp(command, "isFull") == 0) { // Checks whether the circular queue is full or not. |
| 106 | + EXPECT_EQ_INT(strcmp(expect, "true") == 0, myCircularQueueIsFull(obj)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // 需要手动释放一下 |
| 111 | + myCircularQueueFree(obj); |
| 112 | + |
| 113 | + arrayFree(expectEntry); |
| 114 | + arrayFree(commandEntry); |
| 115 | + arrayFree(argEntry); |
| 116 | +} |
| 117 | + |
| 118 | +int main(void) { |
| 119 | + test("[null, true, true, true, false, 3, true, true, true, 4]", |
| 120 | + "[MyCircularQueue, enQueue, enQueue, enQueue, enQueue, Rear, isFull, deQueue, enQueue, Rear]", |
| 121 | + "[[3], [1], [2], [3], [4], [], [], [], [4], []]"); |
| 122 | + |
| 123 | + return testOutput(); |
| 124 | +} |
0 commit comments