栈实现队列 和 队列实现栈 -- C语言

栈实现队列

–> leetcode 链接
在这里插入图片描述

先看这道题
入数据很简单
在这里插入图片描述
但是如果要出数据就要先把数据从第一个栈 pop出来,之后 push 到 另外一个栈中
在这里插入图片描述
然后就可以把第二个栈中的元素pop出去,就可以啦。

源码

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
bool StackEmpty(ST* ps);


void StackInit(ST* ps)
{
	assert(ps);
	ps->capacity = 0;
	ps->a = NULL;
	ps->top = 0;
}

void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = realloc(ps->a, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newCapacity;
		ps->a = tmp;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

void StackDesroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}

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

	return ps->top;
}

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

typedef struct 
{
    ST stPush;
    ST stPop;
} MyQueue;

bool myQueueEmpty(MyQueue* obj);

MyQueue* myQueueCreate() {
    MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->stPush);
    StackInit(&q->stPop);
    return q;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->stPush, x);
}

int myQueuePop(MyQueue* obj) {
    if(StackEmpty(&obj->stPop))
    {
    while(!StackEmpty(&obj->stPush))//把 stPush 的元素都放到 stPop中
    {
    StackPush(&obj->stPop, StackTop(&obj->stPush));
    StackPop(&obj->stPush);
    }
    }
    int ret = StackTop(&obj->stPop);
    StackPop(&obj->stPop);
    return ret;
}

int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(&obj->stPop))
    {    
    while(!StackEmpty(&obj->stPush))//把 stPush 的元素都放到 stPop中
    {
    StackPush(&obj->stPop, StackTop(&obj->stPush));
    StackPop(&obj->stPush);
    }
    }
    
    return StackTop(&obj->stPop);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->stPop) && StackEmpty(&obj->stPush);
}

void myQueueFree(MyQueue* obj) {
    StackDesroy(&obj->stPush);
    StackDesroy(&obj->stPop);
    free(obj);
}

队列实现栈

–> leetcode 链接
在这里插入图片描述

这道题也是入数据很简单
在这里插入图片描述

但是出数据就和上一道题有些不同了,我们可以先把第一个队列中的数据 pop 到第二个
直到只剩下一个元素,然后把这个元素 pop 掉, 就达到 后入先出 的目的了
在这里插入图片描述
源码

typedef int QueueDatatype;
typedef struct QListNode
{
	struct QListNode* next;
	QueueDatatype data;
}QNode;


typedef struct Queue
{
	QNode* head;
	QNode* tail;
}Queue;

bool QueueEmpty(Queue* pq);


void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;

}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while(cur != NULL)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->tail = pq->head = NULL;
}

void QueuePush(Queue* pq, QueueDatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	QNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = NULL;
	}
}

QueueDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

QueueDatatype QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	int count = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* st = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&st->q1);
    QueueInit(&st->q2);
    return st;
}

void myStackPush(MyStack* obj, int x) {
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1, x);
    }
    else
    {
        QueuePush(&obj->q2, x);
    }
}

int myStackPop(MyStack* obj) {
    Queue* emptyQ = &obj->q1;
    Queue* nonemptyQ = &obj->q2;
    if(!QueueEmpty(&obj->q1))//转化 空 和 非空 队列
    {
        emptyQ = &obj->q2;
        nonemptyQ = &obj->q1;
    }

    while(QueueSize(nonemptyQ) > 1)
    {
        QueuePush(emptyQ,QueueFront(nonemptyQ));
        QueuePop(nonemptyQ);
    }
    int top = QueueFront(nonemptyQ);
    QueuePop(nonemptyQ);

    return top;
}

int myStackTop(MyStack* obj) {
    if(!QueueEmpty(&obj->q1))
    {
        return QueueBack(&obj->q1);
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->q1);
    QueueDestroy(&obj->q2);
    free(obj);
}

真不戳

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

eptcup_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值