【LeetCode】Easy | 232. 用栈实现队列 (纯c手撕栈)

题目

【LeetCode】Easy | 232. 用栈实现队列 (纯c手撕栈)_第1张图片

思路

两个结构的特点:① :尾插 + 头删 ② :尾插 + 尾删
尾插的思路:若两个栈都为空,随便选一个栈Push;若两个栈其中有一个不为空,那么选不为空的栈Push
头删的思路:找到不为空的栈 --> 将前 n − 1 n − 1 n1 个元素Push到空栈中 --> 将第n 个元素Pop --> 由于push到空栈的顺序与原顺序相反所以将剩下的元素Push到现在的空栈中

与题【225. 用队列实现栈】不同的是,这个需要反转一次。

代码

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
typedef struct {
    ST s1;
    ST s2;
} MyQueue;
//初始化
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
//入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	//判断空间是否足够
	//空间不足
	if (ps->top == ps->capacity)
	{
		int newCapacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
		STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newCapacity);
		//开辟失败
		if (tmp == NULL)
		{
			printf("realloc fail!");
			exit(0);
		}
		//开辟成功
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
//出栈
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
//栈的大小
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;//注意是从0开始的
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;//==0是真返回true;!=0是假返回false
}
//栈的销毁
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}


MyQueue* myQueueCreate() {
    MyQueue* mq=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&mq->s1);
    StackInit(&mq->s2);
    return mq;
}

void myQueuePush(MyQueue* obj, int x) {
    if(StackEmpty(&obj->s1))
    {
        StackPush(&obj->s2,x);
    }
    else
    {
        StackPush(&obj->s1,x);
    }
}

int myQueuePop(MyQueue* obj) {
    ST *emptyS=&obj->s1;
    ST* noEmptyS=&obj->s2;
    if(!StackEmpty(emptyS))
    {
        emptyS=&obj->s2;
        noEmptyS=&obj->s1;
    }
    while(StackSize(noEmptyS)>1)
    {
        StackPush(emptyS,StackTop(noEmptyS));
        StackPop(noEmptyS);
    }
    int top=StackTop(noEmptyS);
    StackPop(noEmptyS);
    emptyS=&obj->s1;
    noEmptyS=&obj->s2;
    if(!StackEmpty(emptyS))
    {
        emptyS=&obj->s2;
        noEmptyS=&obj->s1;
    }
     while(StackSize(noEmptyS)>0)
    {
        StackPush(emptyS,StackTop(noEmptyS));
        StackPop(noEmptyS);
    }
    return top;
}

int myQueuePeek(MyQueue* obj) {
     ST *emptyS=&obj->s1;
    ST* noEmptyS=&obj->s2;
    if(!StackEmpty(emptyS))
    {
        emptyS=&obj->s2;
        noEmptyS=&obj->s1;
    }
    while(StackSize(noEmptyS)>0)
    {
        StackPush(emptyS,StackTop(noEmptyS));
        StackPop(noEmptyS);
    }
    int top=StackTop(emptyS);
    emptyS=&obj->s1;
    noEmptyS=&obj->s2;
        if(!StackEmpty(emptyS))
    {
        emptyS=&obj->s2;
        noEmptyS=&obj->s1;
    }
     while(StackSize(noEmptyS)>0)
    {
        StackPush(emptyS,StackTop(noEmptyS));
        StackPop(noEmptyS);
    }
    return top;
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->s1);
    StackDestroy(&obj->s2);
    free(obj);
}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/

你可能感兴趣的:(#,LeetCode,leetcode,c语言,算法)