用队列实现栈

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

实现 MyStack 类:

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

注意:

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

示例:

输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]

解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

提示:

  • 1 <= x <= 9
  • 最多调用100 次 pushpoptop 和 empty
  • 每次调用 pop 和 top 都保证栈不为空

进阶:你能否仅用一个队列来实现栈。

队列代码

这里我采用的是c语言,没有库手撕了队列代码如下

typedef int QueueData;
struct QueueNode
{
	QueueData val;
	struct QueueNode* next;
};
typedef struct QueueNode QNode;
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Que;
void QueueInit(Que* pq);//队列初始化
void QueueDestroy(Que* pq);//队列销毁

void QueuePush(Que* pq,QueueData x);//入队列
void QueuePop(Que* pq);//出队列

QueueData QueueBack(Que* pq);
QueueData QueueFront(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);

void QueueInit(Que* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
void QueueDestroy(Que* pq)
{
	QNode* pcur = pq->phead;
	while (pcur)
	{
		QNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;	
}

void QueuePush(Que* pq,QueueData x)
{
	assert(pq);
	QNode* newnode = (Que*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->ptail)
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	else
	{
		pq->phead = pq->ptail = newnode;
	}
	pq->size++;
}
void QueuePop(Que* pq)
{
	assert(pq->phead != NULL);

	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--;
}

QueueData QueueFront(Que* pq)
{
	assert(pq!=NULL);

	assert(pq->phead != NULL);
	return pq->phead->val;
}
QueueData QueueBack(Que* pq)
{
	assert(pq!=NULL);

	assert(pq->ptail != NULL);
	return pq->ptail->val;
}
bool QueueEmpty(Que* pq)
{
	return pq->phead == NULL;
}
int QueueSize(Que* pq)
{
	assert(pq);
	return pq->size;

}

分析

 题目要求我们实现这些代码,但栈和队列又是相反并且题目要求用俩个队列去实现栈

上图是题目中给的结构体这里的结构体没有名字这里是匿名结构体,用下面那个名字就行,这里我们给结构体的成员加上我们俩个队列 

 如下图Que这里定义的是phead 和ptail俩个节点,这俩个节点的类型又是Qnode类型

 Qnode的底层又是一个链表

 也就是说他们是一种嵌套关系,下图的1,2,3则是链表

 

这样第一个接口就写好了 

1.MyStack

2.MyStack* myStackCreate

这里用的是一个函数初始化并且返回指针,记住不要用局部变量创建然后返回,值可以正常返回但是返回了以后原来在栈区创建的变量已经销毁就变成了野指针所这里用堆区的malloc然后初始化

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

3. void myStackPush

因为队列和栈是相对的一个是先进后出一个是后进先出这里看哪一个队列是空的就插入哪一个

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

 4. int myStackPop

首先要明白队列和栈是相对的,那么出栈的时他们俩个就是这样的

 

如果用用俩个队列去模拟栈那么如下图,如果要把pop  4那么就需要先把1,2,3这n-1

的数据移动到第一个队列中,然后再把4释放掉 

 

 

 这样就出栈了

int myStackPop(MyStack* obj) {
    Que* pEmptyQ=&obj->q1;
    Que* pNonEmptyQ=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        pEmptyQ=&obj->q2;
        pNonEmptyQ=&obj->q1;
    }
    while(QueueSize(pNonEmptyQ)>1)
    {
        int front=QueueFront(pNonEmptyQ);
        QueuePush(pEmptyQ,front);
        QueuePop(pNonEmptyQ);
        
    }
    int front=QueueFront(pNonEmptyQ);

    QueuePop(pNonEmptyQ);
    return front;

}

 

5.int myStackTop

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

5.bool myStackEmpty

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

7.void myStackFree

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

    free(obj);
}

完整代码

typedef int QueueData;
struct QueueNode
{
	QueueData val;
	struct QueueNode* next;
};
typedef struct QueueNode QNode;
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Que;
void QueueInit(Que* pq);//队列初始化
void QueueDestroy(Que* pq);//队列销毁

void QueuePush(Que* pq,QueueData x);//入队列
void QueuePop(Que* pq);//出队列

QueueData QueueBack(Que* pq);
QueueData QueueFront(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);

void QueueInit(Que* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
void QueueDestroy(Que* pq)
{
	QNode* pcur = pq->phead;
	while (pcur)
	{
		QNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;	
}

void QueuePush(Que* pq,QueueData x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->ptail)
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	else
	{
		pq->phead = pq->ptail = newnode;
	}
	pq->size++;
}
void QueuePop(Que* pq)
{
	assert(pq->phead != NULL);

	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--;
}

QueueData QueueFront(Que* pq)
{
	assert(pq!=NULL);

	assert(pq->phead != NULL);
	return pq->phead->val;
}
QueueData QueueBack(Que* pq)
{
	assert(pq!=NULL);

	assert(pq->ptail != NULL);
	return pq->ptail->val;
}
bool QueueEmpty(Que* pq)
{
	return pq->phead == NULL;
}
int QueueSize(Que* pq)
{
	assert(pq);
	return pq->size;

}

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


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

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

int myStackPop(MyStack* obj) {
    Que* pEmptyQ=&obj->q1;
    Que* pNonEmptyQ=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        pEmptyQ=&obj->q2;
        pNonEmptyQ=&obj->q1;
    }
    while(QueueSize(pNonEmptyQ)>1)
    {
        int front=QueueFront(pNonEmptyQ);
        QueuePush(pEmptyQ,front);
        QueuePop(pNonEmptyQ);
        
    }
    int front=QueueFront(pNonEmptyQ);

    QueuePop(pNonEmptyQ);
    return front;

}

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);
}

/**
 * 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);
*/

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值