力扣225. 用队列实现栈

题目

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

题解

将元素压入栈顶,即将元素插入队尾,注意要插入到不为空的队列中,如果两个队列均为空,随便插入一个队列即可。取栈顶元素时,将不空队列中的元素插入空队列中,当不空队列中只剩下一个元素时,该元素为栈顶元素。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <errno.h>

typedef int QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);

//队列初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

//队列销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

//入队
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		assert(pq->phead == NULL);
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

//出队
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->phead->next == NULL)
	{
		//单个节点,直接删除
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		//多个节点,相当于头删
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}

	pq->size--;
}

//队头元素
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

//队尾元素
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->data;
}

//队列长度
int QueueSize(Queue* pq)
{
	assert(pq);
	
	return pq->size;
}

//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	
	return pq->phead == NULL && pq->ptail == NULL;
}

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


MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));

    QueueInit(&obj->q1);
    QueueInit(&obj->q2);

    return obj; 
}

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

int myStackPop(MyStack* obj) {
    Queue* pEmptyQ = &obj->q1;
    Queue* pNonEmptyQ = &obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        pEmptyQ = &obj->q2;
        pNonEmptyQ = &obj->q1;
    }
    while(QueueSize(pNonEmptyQ) > 1)
    {
        QueuePush(pEmptyQ,QueueFront(pNonEmptyQ));
        QueuePop(pNonEmptyQ);
    }
    int top = QueueFront(pNonEmptyQ);
    QueuePop(pNonEmptyQ);
    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);
}

相关推荐

  1. 225. 队列实现

    2024-01-04 13:10:05       52 阅读
  2. [题解]225. 队列实现

    2024-01-04 13:10:05       31 阅读
  3. 225队列实现 记录

    2024-01-04 13:10:05       26 阅读
  4. 232. 实现队列

    2024-01-04 13:10:05       53 阅读
  5. 队列实现-

    2024-01-04 13:10:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-04 13:10:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-04 13:10:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-04 13:10:05       87 阅读
  4. Python语言-面向对象

    2024-01-04 13:10:05       96 阅读

热门阅读

  1. cesium监听23D切换

    2024-01-04 13:10:05       60 阅读
  2. 常用日志查看方法 log | journalctl | messages

    2024-01-04 13:10:05       60 阅读
  3. Locust:分布式负载测试工具的利器

    2024-01-04 13:10:05       59 阅读
  4. Go到底能做什么?不能做什么?

    2024-01-04 13:10:05       56 阅读
  5. mysql常用命令

    2024-01-04 13:10:05       49 阅读
  6. Go语言的几种类型转换

    2024-01-04 13:10:05       61 阅读
  7. Go编程的一些最佳实践

    2024-01-04 13:10:05       45 阅读
  8. Go 语言教程

    2024-01-04 13:10:05       58 阅读
  9. kotlin foreach 循环

    2024-01-04 13:10:05       52 阅读
  10. 开源字符识别 OCR 引擎推荐

    2024-01-04 13:10:05       54 阅读