【力扣】用队列实现栈

225. 用队列实现栈 - 力扣(LeetCode)

题目:

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

实现 MyStack 类:

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

注意:

你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

思路:

队列的特点是先进先出,栈的特点是先进后出

本题需要用到两个队列

 

代码:

typedef int QueueDataType;
typedef struct QueueNode
{
	QueueDataType data;
	struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
	QueueNode* head;
	QueueNode* tail;

}Queue;
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* temp = cur->next;
		free(cur);
		cur = temp;
	}
	pq->head = NULL;
	pq->tail = NULL;
}
//尾进头出
void QueuePush(Queue* pq, QueueDataType x)
{
	assert(pq);
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	assert(newNode);
	newNode->data = x;
	newNode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newNode;
	}
	else
	{
		pq->tail->next = newNode;
		pq->tail = newNode;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);

	if (pq->head->next)
	{
		QueueNode* headNext = pq->head->next;
		free(pq->head);
		pq->head = headNext;
	}
	else
	{
		free(pq->head);
		pq->head = NULL;
		pq->tail = NULL;
	}
}
size_t QueueSize(Queue* pq)
{
	size_t size = 0;
	QueueNode* cur = pq->head;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}
bool QueueEmpty(Queue* pq)
{
	return pq->head == NULL;
}
QueueDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
QueueDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;
}

typedef struct
{
	Queue Q1;
	Queue Q2;
} MyStack;


MyStack* myStackCreate()
{
	MyStack* stack = (MyStack*)malloc(sizeof(MyStack));
	QueueInit(&stack->Q1);
	QueueInit(&stack->Q2);
	return stack;
}

void myStackPush(MyStack* obj, int x)
{
	Queue* qNotNull = obj->Q1.head != NULL ? &obj->Q1 : &obj->Q2;//不为空的队列
	QueuePush(qNotNull, x);
}

int myStackPop(MyStack* obj)
{
	Queue* qNotNull ;
	Queue* qNull;
	if (obj->Q1.head)
	{
		qNotNull = &obj->Q1;
		qNull = &obj->Q2;
	}
	else
	{
		qNotNull = &obj->Q2;
		qNull = &obj->Q1;
	}
	QueueDataType temp = 0;
	while (qNotNull->head != qNotNull->tail)
	{
		temp = qNotNull->head->data;
		QueuePop(qNotNull);
		QueuePush(qNull, temp);
	}
	temp = qNotNull->head->data;
	QueuePop(qNotNull);
	return temp;
}

int myStackTop(MyStack* obj)
{
	Queue qNotNull;
	Queue qNull;
	if (obj->Q1.head)
	{
		qNotNull = obj->Q1;
		qNull = obj->Q2;
	}
	else
	{
		qNotNull = obj->Q2;
		qNull = obj->Q1;
	}
	QueueDataType temp = 0;

	temp = qNotNull.tail->data;

	return temp;
}

bool myStackEmpty(MyStack* obj)
{
	if (obj->Q1.head == NULL && obj->Q2.head == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void myStackFree(MyStack* obj)
{
	QueueDestroy(&obj->Q1);
	QueueDestroy(&obj->Q2);
}

<think>好的,我现在需要帮助用户找到LeetCode第111题的C语言解决方案。首先,我得确认题目内容。LeetCode 111题是“二叉树的最小深度”,要求找出从根节点到最近叶子节点的最短路径上的节点数量。 用户明确要求用C语言实现,所以我需要回忆或查找相关的解决方案。根据之前的引用,解决方案通常包括递归、迭代和使用Morris遍历。过Morris遍历在C语言中可能比较复杂,而递归和迭代方法更常见。 首先考虑递归方法。二叉树的最小深度是简单的取左右子树深度的较小值,因为如果某子树为空,就能取它,否则会得到错误的结果。例如,当左子树为空时,最小深度应取决于右子树。因此,递归的终止条件需要处理节点为空的情况,以及左右子树是否存在的情况。 接下来是迭代方法,通常使用层序遍历(BFS),这样可以逐层检查,一旦遇到叶子节点就返回当前深度,这样效率更高,因为需要遍历整个树。这在C语言中可以通过队列实现,但需要手动实现队列结构,因为C没有内置的数据结构。 另外,用户提供的引用中提到递归和迭代的时间复杂度都是O(n),空间复杂度递归为O(n)(调用),而迭代的队列也是O(n)。Morris遍历可以达到O(1)的空间,但实现起来较为复杂,可能在面试中太常见,但用户可能希望了解所有方法。 现在需要编写C语言的代码。首先处理递归方法。结构体TreeNode的定义是必要的。递归函数需要考虑左右子树是否存在的情况。如果左子树存在,就递归右子树;反之亦然。如果都存在,取较小值。 然后是迭代方法,使用队列进行层序遍历。每次处理一层节点,遇到第一个叶子节点就返回当前深度。C语言中需要自己实现队列,可能用数组或链表。需要注意内存分配和释放,避免泄漏。 还需要注意测试用例,例如树为空的情况,或者只有左子树或右子树的情况。确保代码在这些情况下都能正确运行。 现在,整理思路,写出递归和迭代的C代码。同时,根据用户要求,生成相关问题,帮助用户进一步理解题目和相关知识点。</think>### LeetCode 111 二叉树的最小深度(C语言实现) #### 题目描述 给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量[^1]。 --- #### 方法1:递归解法 **核心思路** 递归遍历左右子树,注意处理单边子树为空的情况(例如左子树为空时,最小深度由右子树决定)。 ```c #include <stdlib.h> #include <math.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int minDepth(struct TreeNode* root) { if (root == NULL) return 0; if (root->left == NULL) return 1 + minDepth(root->right); // 左子树为空 if (root->right == NULL) return 1 + minDepth(root->left); // 右子树为空 return 1 + fmin(minDepth(root->left), minDepth(root->right)); // 左右子树均非空 } ``` **复杂度分析** - 时间复杂度:$O(n)$,每个节点遍历一次 - 空间复杂度:$O(h)$,递归深度与树高$h$相关[^1] --- #### 方法2:迭代解法(层序遍历) **核心思路** 使用队列实现BFS,首次遇到叶子节点时立即返回当前层数。 ```c #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; typedef struct QueueNode { struct TreeNode* tree_node; int depth; struct QueueNode* next; } QueueNode; int minDepth(struct TreeNode* root) { if (root == NULL) return 0; QueueNode* queue = (QueueNode*)malloc(sizeof(QueueNode)); queue->tree_node = root; queue->depth = 1; queue->next = NULL; QueueNode* front = queue; QueueNode* rear = queue; while (front != NULL) { struct TreeNode* current = front->tree_node; int current_depth = front->depth; if (current->left == NULL && current->right == NULL) { free(queue); return current_depth; // 找到第一个叶子节点 } if (current->left != NULL) { rear->next = (QueueNode*)malloc(sizeof(QueueNode)); rear = rear->next; rear->tree_node = current->left; rear->depth = current_depth + 1; rear->next = NULL; } if (current->right != NULL) { rear->next = (QueueNode*)malloc(sizeof(QueueNode)); rear = rear->next; rear->tree_node = current->right; rear->depth = current_depth + 1; rear->next = NULL; } QueueNode* temp = front; front = front->next; free(temp); } return 0; // 会执行到此 } ``` **复杂度分析** - 时间复杂度:$O(n)$,每个节点入队一次 - 空间复杂度:$O(n)$,队列存储节点数量[^1] ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值