力扣225-用队列实现栈(C语言版)

在这里插入图片描述
要用队列实现栈,那我们首先应该明白的是实现栈的目的就是实现它的几个操作:压栈、弹栈、取栈顶元素、判空、求元素个数。那我们现在将每个操作细化来看应该如何实现。

1、压栈:将元素尾插到栈中,队列的入队操也是将元素尾插队中。用队列的入队操作是可以实现的压栈的。

2、取栈顶元素:取栈顶元素实际上就是取队尾的元素。可以用队列直接实现。

3、判空:队列空了栈也就是空的。可以用队列直接实现。

4、求元素个数:求队列中的元素个数即是栈的元素个数。可以用队列直接实现。

5、弹栈:将栈顶元素删除就是删除队尾的元素。队列是先进先出只能删除队头元素队列无法实现。

到这里我们就会发现要用队列实现栈,前四个操作都是可以直接用队列实现的,而弹栈操作却不能直接用队列实现,那其实这也正是栈(后进先出)和队列(先进先出)最本质的区别了。
因此要用队列实现栈无非就是实现弹栈操作了。很显然,一个队列是无法实现的,那两个队列呢。

在这里插入图片描述

代码的实现

类型定义和创建

typedef struct {
    
    
    Queue _q1;//用两个队列实现栈,可以实现弹栈操作
    Queue _q2;//这两个队列一个是保存元素另一个是空的
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    
    
    MyStack *newStack = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&(newStack->_q1));
    QueueInit(&(newStack->_q2));//初始化两个队列
    return newStack;
}

压栈

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    
    
    //将元素压入不是空的那个队列当中(可能是队列1也可能是队列2))
    if(!QueueEmpty(&(obj->_q1)))
    {
    
    
        QueuePush(&(obj->_q1),x);
    }
    else
    {
    
    
        QueuePush(&(obj->_q2),x);
    }
}

弹栈

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    
    
    if(!QueueEmpty( &(obj->_q1) ))//元素保存在q1当中
    {
    
    
        while(QueueSize( &(obj->_q1) ) > 1)
        {
    
    
            QueuePush( &(obj->_q2) , QueueFront( &(obj->_q1) ));//将q1队头元素进入q2中
            QueuePop( &(obj->_q1) );//删除q1队头元素
        }//将q1元素倒入q2。q1的队尾元素不倒
        int ret = QueueFront( &(obj->_q1) );//记录q1的队尾元素
        QueuePop( &(obj->_q1) );//删除q1的队尾元素
        return ret;
    }
    else//当元素保存在q2当中时
    {
    
    
        while(QueueSize( &(obj->_q2) ) > 1)
        {
    
    
            QueuePush( &(obj->_q1) , QueueFront( &(obj->_q2) ));//将q2队头元素进入q1中
            QueuePop( &(obj->_q2) );//删除q2队头元素
        }//将q2元素倒入q1。q2的队尾元素不倒
        int ret = QueueFront( &(obj->_q2) );//记录q2的队尾元素
        QueuePop( &(obj->_q2) );//删除q2的队尾元素
        return ret;
    }
}

弹栈操作的另外一种代码

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj){
    
    
    Queue *empty = &(obj->_q1);
    Queue *nonEmpty = &(obj->_q2);
    if(!QueueEmpty( &(obj->_q1) ))
    {
    
    
        nonEmpty = &(obj->_q1);
        empty = &(obj->_q2);
    }//empty指向的是空的队列,nonEmpty指向的是非空队列
    while(QueueSize(nonEmpty) > 1)
    {
    
    
        QueuePush(empty,QueueFront(nonEmpty));
        QueuePop(nonEmpty);
    }//将非空队列除队尾元素其余元素压入空队列
    int ret = QueueFront(nonEmpty);//记录队尾元素
    QueuePop(nonEmpty);//删除队尾元素
    return ret;
}

取栈顶元素

/** Get the top element. */
int myStackTop(MyStack* obj) {
    
    
    if(!QueueEmpty( &(obj->_q1) ))
    {
    
    
        return QueueBack(&obj->_q1);//返回队列的队尾元素
    }
    else
    {
    
    
        return QueueBack(&obj->_q2);
    }
}

判空

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    
    
    if(QueueEmpty(&(obj->_q1)) && QueueEmpty(&(obj->_q2)))
    {
    
    
        return true;
    }//只有当两个队列都是空的时候栈才是空的
    else
    {
    
    
        return false;
    }
}

销毁

void myStackFree(MyStack* obj) {
    
    
    QueueDestroy( &(obj->_q1) );
    QueueDestroy( &(obj->_q2) );//将队列销毁
    free(obj);//销毁掉我们自己开辟的空间
}

要用队列实现栈首先要有队列

队列的实现

typedef int QDataType;

typedef struct QueueNode
{
    
    
	QDataType _data;
	struct QueueNode *_next;
}QueueNode;

typedef struct Queue
{
    
    
	QueueNode *_head;
	QueueNode *_tail;
}Queue;

//初始化
void QueueInit(Queue *pq);
//入队
void QueuePush(Queue *pq, QDataType x);
//出队
void QueuePop(Queue *pq);
//取队头元素
QDataType QueueFront(Queue *pq);
//取队尾元素
QDataType QueueBack(Queue* pq);
//判空
int QueueEmpty(Queue *pq);
//求元素个数
int QueueSize(Queue *pq);
//销毁
void QueueDestroy(Queue *pq);


//初始化
void QueueInit(Queue *pq)
{
    
    
	assert(pq);
	pq->_head = pq->_tail = NULL;
}
static QueueNode *BuyQueueNode()
{
    
    
	QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
    
    
		printf("malloc error!\n");
		exit(-1);
	}
	return newNode;
}
//入队
void QueuePush(Queue *pq, QDataType x)
{
    
    
	assert(pq);
	QueueNode *newNode = BuyQueueNode();
	newNode->_data = x;
	newNode->_next = NULL;
	if (pq->_head == NULL)
	{
    
    
		pq->_head = newNode;
		pq->_tail = newNode;
	}
	else
	{
    
    
		pq->_tail->_next = newNode;
		pq->_tail = newNode;
	}
}
//出队
void QueuePop(Queue *pq)
{
    
    
	assert(pq);
	QueueNode *next = pq->_head->_next;
	free(pq->_head);
	pq->_head = next;
}
//取队头元素
QDataType QueueFront(Queue *pq)
{
    
    
	return pq->_head->_data;
}
//取队尾元素
QDataType QueueBack(Queue* pq)
{
    
    
	return pq->_tail->_data;
}
//判空
int QueueEmpty(Queue *pq)
{
    
    
	return pq->_head == NULL ? 1 : 0;
}
//求元素个数
int QueueSize(Queue *pq)
{
    
    
	assert(pq);
	QueueNode *cur = pq->_head;
	int size = 0;
	while (cur)
	{
    
    
		size++;
		cur = cur->_next;
	}
	return size;
}
//销毁
void QueueDestroy(Queue *pq)
{
    
    
	assert(pq);
	
	while (pq->_head)
	{
    
    
		QueueNode *del = pq->_head;
		pq->_head = pq->_head->_next;
		free(del);
	}
	pq->_head = pq->_tail = NULL;
}

猜你喜欢

转载自blog.csdn.net/weixin_50168448/article/details/112262220
今日推荐