This commit is contained in:
2025-07-12 23:22:19 +08:00
parent 98b0294f80
commit 321cb6c439
3 changed files with 157 additions and 0 deletions

36
include/solution/232.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef INC_232_H
#define INC_232_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
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

72
src/232.c Normal file
View File

@@ -0,0 +1,72 @@
#include <solution/232.h>
#include <stdlib.h>
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);
}

49
tests/test_232.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include <solution/232.h>
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);
}