栈实现队列

栈实现队列

     力扣链接:栈实现队列

     csdn:队列实现栈链接
在这里插入图片描述
这里的思路和队列实现栈的思路大同小异

思路:创建两个栈用来倒数据,用它们其中一个来反转元素的入队顺序,用另一个来存储元素的最终顺序。

  • 一个pushst(用来入栈),一个popst(用来出栈

push接口:
   直接在pushst中入就行,栈就是先进后出

在这里插入图片描述

peek接口:
   这个接口获取的是队头数据,我们需要把pushst中的数据转移到popst中用到的接口是StackPushStackTop(获取栈顶数据把数据转到popst中)

在这里插入图片描述

  这样队列头的数据就会到栈顶,在取栈顶数据就可以了

pop接口:
   直接取栈顶元素保存,然后pop掉栈顶元素,在返回就行

empty接口:
   两个栈都为NULL才是空。为空返回true,否则返回false

代码:

typedef struct {
    ST pushst;
    ST popst;
} MyQueue;

bool myQueueEmpty(MyQueue* obj);
MyQueue* myQueueCreate() {
    MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&pq->pushst);
    StackInit(&pq->popst);

    return pq;
}   

void myQueuePush(MyQueue* obj, int x) {
    assert(obj);

    StackPush(&obj->pushst, x);
}

int myQueuePop(MyQueue* obj) {
    assert(obj);
    assert(!myQueueEmpty(obj));

    int peek = myQueuePeek(obj);
    StackPop(&obj->popst);

    return peek;
}

int myQueuePeek(MyQueue* obj) {
    assert(obj);
    assert(!(myQueueEmpty(obj)));
    //popst为空就倒数据
    if(StackEmpty(&obj->popst))
    {
        while(!StackEmpty(&obj->pushst))
        {
            StackPush(&obj->popst, StackTop(&obj->pushst));
            StackPop(&obj->pushst);
        }
    }

    return StackTop(&obj->popst);
}

bool myQueueEmpty(MyQueue* obj) {
    assert(obj);

    return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
    assert(obj);
    StackDestroy(&obj->pushst);
    StackDestroy(&obj->popst);

    free(obj);

}

最后的free接口要注意,obj是MyQueue*指针,它指向的空间还有2个ST类型的空间也是需要释放的
在这里插入图片描述

整体代码:

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}ST;

int StackSize(ST* ps);
bool StackEmpty(ST* ps);
void StackInit(ST* ps)
{
	assert(ps);

	ps->a = (STDatatype*)malloc(sizeof(STDatatype)* 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	ps->top = 0;
	ps->capacity = 4;
}

void StackDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void StackPush(ST* ps, STDatatype x)
{
	assert(ps);

	// 
	if (ps->top == ps->capacity)
	{
		STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

bool StackEmpty(ST* ps)
{
	assert(ps);

	/*if (ps->top == 0)
	{
	return true;
	}
	else
	{
	return false;
	}*/

	return ps->top == 0;
}

int StackSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

typedef struct {
    ST pushst;
    ST popst;
} MyQueue;

bool myQueueEmpty(MyQueue* obj);
MyQueue* myQueueCreate() {
    MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&pq->pushst);
    StackInit(&pq->popst);

    return pq;
}   

void myQueuePush(MyQueue* obj, int x) {
    assert(obj);

    StackPush(&obj->pushst, x);
}

int myQueuePop(MyQueue* obj) {
    assert(obj);
    assert(!myQueueEmpty(obj));

    int peek = myQueuePeek(obj);
    StackPop(&obj->popst);

    return peek;
}

int myQueuePeek(MyQueue* obj) {
    assert(obj);
    assert(!(myQueueEmpty(obj)));
    //popst为空就倒数据
    if(StackEmpty(&obj->popst))
    {
        while(!StackEmpty(&obj->pushst))
        {
            StackPush(&obj->popst, StackTop(&obj->pushst));
            StackPop(&obj->pushst);
        }
    }

    return StackTop(&obj->popst);
}

bool myQueueEmpty(MyQueue* obj) {
    assert(obj);

    return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
    assert(obj);
    StackDestroy(&obj->pushst);
    StackDestroy(&obj->popst);

    free(obj);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值