diff --git a/include/solution/232.h b/include/solution/232.h new file mode 100644 index 0000000..f48900f --- /dev/null +++ b/include/solution/232.h @@ -0,0 +1,36 @@ +#ifndef INC_232_H +#define INC_232_H +#ifdef __cplusplus +extern "C" +{ +#endif +#include + + typedef struct + { + int *base; + int ptr; + int size; + } stack; + + typedef struct + { + stack *s1; + stack *s2; + } MyQueue; + + MyQueue *myQueueCreate(); + + void myQueuePush(MyQueue *obj, int x); + + int myQueuePop(MyQueue *obj); + + int myQueuePeek(MyQueue *obj); + + bool myQueueEmpty(MyQueue *obj); + + void myQueueFree(MyQueue *obj); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/src/232.c b/src/232.c new file mode 100644 index 0000000..dcbe432 --- /dev/null +++ b/src/232.c @@ -0,0 +1,72 @@ +#include +#include + +MyQueue *myQueueCreate() +{ + MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue)); + q->s1 = (stack *)malloc(sizeof(stack)); + q->s2 = (stack *)malloc(sizeof(stack)); + + q->s1->size = q->s2->size = 10; + q->s1->ptr = q->s2->ptr = 0; + + q->s1->base = (int *)malloc(sizeof(int) * 10); + q->s2->base = (int *)malloc(sizeof(int) * 10); + + return q; +} + +void myQueuePush(MyQueue *obj, int x) +{ + if (obj->s1->ptr > obj->s1->size) + { + obj->s1->base = (int *)realloc(obj->s1->base, sizeof(int) * (obj->s1->size + 10)); + obj->s2->base = (int *)realloc(obj->s2->base, sizeof(int) * (obj->s1->size + 10)); + obj->s1->size = obj->s2->size = obj->s1->size + 10; + } + obj->s1->base[(obj->s1->ptr)++] = x; +} + +int myQueuePop(MyQueue *obj) +{ + int ret; + + while (obj->s1->ptr) + obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)]; + + ret = obj->s2->base[--(obj->s2->ptr)]; + + while (obj->s2->ptr) + obj->s1->base[(obj->s1->ptr)++] = obj->s2->base[--(obj->s2->ptr)]; + + return ret; +} + +int myQueuePeek(MyQueue *obj) +{ + int ret; + + while (obj->s1->ptr) + obj->s2->base[(obj->s2->ptr)++] = obj->s1->base[--(obj->s1->ptr)]; + + ret = obj->s2->base[obj->s2->ptr - 1]; + + obj->s1->ptr = obj->s2->ptr; + obj->s2->ptr = 0; + + return ret; +} + +bool myQueueEmpty(MyQueue *obj) +{ + return !obj->s1->ptr; +} + +void myQueueFree(MyQueue *obj) +{ + free(obj->s1->base); + free(obj->s2->base); + free(obj->s1); + free(obj->s2); + free(obj); +} \ No newline at end of file diff --git a/tests/test_232.cpp b/tests/test_232.cpp new file mode 100644 index 0000000..d1bd3f6 --- /dev/null +++ b/tests/test_232.cpp @@ -0,0 +1,49 @@ +#include +#include + +class MyQueueTest : public ::testing::Test +{ + protected: + MyQueue *queue; + + void SetUp() override + { + queue = myQueueCreate(); + ASSERT_NE(queue, nullptr); // 确保创建成功 + } + + void TearDown() override + { + myQueueFree(queue); + } +}; + +// Test1 对应你提供的操作序列 +TEST_F(MyQueueTest, BasicOperations) +{ + // ["MyQueue", "push", "push", "peek", "pop", "empty"] + // [[], [1], [2], [], [], []] + + myQueuePush(queue, 1); // queue: [1] + myQueuePush(queue, 2); // queue: [1, 2] + + int peeked = myQueuePeek(queue); // should return 1 + EXPECT_EQ(peeked, 1); + + int popped = myQueuePop(queue); // should return 1, queue becomes [2] + EXPECT_EQ(popped, 1); + + bool isEmpty = myQueueEmpty(queue); // should return false + EXPECT_FALSE(isEmpty); +} + +TEST_F(MyQueueTest, PushPeekEmpty) +{ + myQueuePush(queue, 1); // queue: [1] + + int peeked = myQueuePeek(queue); // should return 1 + EXPECT_EQ(peeked, 1); + + bool isEmpty = myQueueEmpty(queue); // should return false + EXPECT_FALSE(isEmpty); +} \ No newline at end of file