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

题目

在这里插入图片描述

思路

两个结构的特点:① :尾插 + 头删 ② :尾插 + 尾删
尾插的思路:若两个栈都为空,随便选一个栈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);
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiYang-DING

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值