0
点赞
收藏
分享

微信扫一扫

Leetcode 225. 用队列实现栈

简单聊育儿 2022-03-27 阅读 42
typedef int TypeData;

typedef struct QueueNode
{
	TypeData val;
	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 QueuePush(Queue* pq, TypeData x)
{
	assert(pq);
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	assert(node);
	node->val = x;
	node->next = NULL;

	if (pq->head == NULL)
	{
		pq->head = node;
		pq->tail = pq->head;
		return;
	}
	pq->tail->next = node;
	pq->tail = pq->tail->next;
}

TypeData QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->val;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head && pq->tail);
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = NULL;
		pq->tail = NULL;
		return;
	}
	QueueNode* headNext = pq->head->next;
	free(pq->head);
	pq->head = headNext;
	// if(pq->head == NULL)
	// {
	//     pq->tail = NULL;
	// }
}

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

void QueueDestroy(Queue* pq)
{
	assert(pq);

	QueueNode* cur = pq->head;
	while (cur != NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	//第二件事  销毁队列
	pq->head = NULL;
	pq->tail = NULL;
	free(pq);
}

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


MyStack* myStackCreate() {
	MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
	assert(obj);
	obj->q1 = (Queue*)malloc(sizeof(Queue));
	obj->q2 = (Queue*)malloc(sizeof(Queue));
	QueueInit(obj->q1);
	QueueInit(obj->q2);
	return obj;
}

void myStackPush(MyStack* obj, int x) {

	assert(obj);
	if (!QueueEmpty(obj->q1))
	{
		QueuePush(obj->q1, x);
	}
	else
	{
		QueuePush(obj->q2, x);
	}
}

int myStackPop(MyStack* obj) {
	assert(obj);
	TypeData val = 0;
	if (!QueueEmpty(obj->q1))
	{
		while (!QueueEmpty(obj->q1))
		{
			val = QueueFront(obj->q1);
			QueuePop(obj->q1);
			if (!QueueEmpty(obj->q1))
			{
				QueuePush(obj->q2, val);
			}
		}
		return val;
	}
	if (!QueueEmpty(obj->q2))
	{
		while (!QueueEmpty(obj->q2))
		{
			val = QueueFront(obj->q2);
			QueuePop(obj->q2);
			if (!QueueEmpty(obj->q2))
			{
				QueuePush(obj->q1, val);
			}
		}
		return val;
	}

	return val;
}

int myStackTop(MyStack* obj) {
	assert(obj);
	TypeData val = 0;
	if (!QueueEmpty(obj->q1))
	{
		while (!QueueEmpty(obj->q1))
		{
			val = QueueFront(obj->q1);
			QueuePop(obj->q1);

			QueuePush(obj->q2, val);
		}
		return val;
	}
	if (!QueueEmpty(obj->q2))
	{
		while (!QueueEmpty(obj->q2))
		{
			val = QueueFront(obj->q2);
			QueuePop(obj->q2);
			QueuePush(obj->q1, val);
		}
		return val;
	}
	return val;
}

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

void myStackFree(MyStack* obj) {
	assert(obj);
	QueueDestroy(obj->q1);
	QueueDestroy(obj->q2);
	free(obj);
}
/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);
 
 * int param_2 = myStackPop(obj);
 
 * int param_3 = myStackTop(obj);
 
 * bool param_4 = myStackEmpty(obj);
 
 * myStackFree(obj);
*/
举报

相关推荐

0 条评论