请你仅使用两个队列实现一个后入先出(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(双端队列)来模拟一个队列 , 只要是标准的队列操作即可
示例:
输入:
[“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 次 push、pop、top 和 empty
每次调用 pop 和 top 都保证栈不为空
这里有一个动图我一会儿上传到评论区大家可以看一下加深理解
为了满足栈的特性,即最后入栈的元素最先出栈,在使用队列实现栈时,应满足队列前端的元素是最后入栈的元素。可以使用两个队列实现栈的操作,其中 queue1
用于存储栈内的元素,queue2
作为入栈操作的辅助队列
入栈操作时,首先将元素入队到 queue2
然后将 queue1
的全部元素依次出队并入队到 queue2
此时queue2
的前端的元素即为新入栈的元素,再将 queue1
和queue2
互换,则queue1
的元素即为栈内的元素,queue1
的前端和后端分别对应栈顶和栈底
由于每次入栈操作都确保 queue1
的前端元素为栈顶元素,因此出栈操作和获得栈顶元素操作都可以简单实现。出栈操作只需要移除 queue1
的前端元素并返回即可,获得栈顶元素操作只需要获得 queue1
的前端元素并返回即可(不移除元素)
由于 queue1
用于存储栈内的元素,判断栈是否为空时,只需要判断 queue1
是否为空即可
动图链接
动图链接
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<assert.h>
typedef int QueueDataType;
typedef struct QNode
{
struct QNode* next;
QueueDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
QueueDataType size;
}Que;
void QueueInit(Que* pq);
void QueuePush(Que* pq, QueueDataType x);
void QueuePop(Que* pq);
void QueueDestroy(Que* pq);
QueueDataType QueueFront(Que* pq);
QueueDataType QueueBack(Que* pq);
bool QueueEmpty(Que* pq);
QueueDataType QueueSize(Que* pq);
//#include"Queue.h"
void QueueInit(Que* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
pq->size =0;
}
void QueuePush(Que* pq, QueueDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof
(QueueNode));
if (newnode == NULL)
{
perror("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->head == NULL && pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;//tail指针指向下一个结构体---
//--存放地址
pq->tail = newnode;//尾节点
}
pq->size++;
}
void QueuePop(Que* pq)
{
assert(pq);
//assert(!QueueEmpty(Que * pq));//写法错误 不允许使用类命名
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
pq->size--;
}
QueueDataType QueueFront(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QueueDataType QueueBack(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
bool QueueEmpty(Que* pq)
{
assert(pq);
return pq->head == NULL;
}
QueueDataType QueueSize(Que* pq)
{
assert(pq);
return pq->size;
}
void QueueDestroy(Que* pq)
{
assert(pq);
//assert(!QueueEmpty(pq));
QueueNode* current = pq->head;
while (current)
{
QueueNode* next = current->next;
free(current)
;
current = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
typedef struct {
Que q1;
Que q2;
}MyStack;
MyStack* myStackCreate() {
//动态内存开辟函数malloc在堆区 无free不会主动销毁
MyStack*pst=(MyStack*)malloc(sizeof(MyStack)*2);
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
//避免野指针NULL
}
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
int myStackPop(MyStack* obj) {
Que*Empty=&obj->q1;
Que*NonEmpty=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
NonEmpty=&obj->q1;
Empty=&obj->q2;
}
while(QueueSize(NonEmpty)>1)
{
QueuePush(Empty,QueueFront(NonEmpty));
QueuePop(NonEmpty);
}
//题目要求移除并返回栈顶元素top
int top=QueueFront(NonEmpty);
QueuePop(NonEmpty);
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);
obj=NULL;
}
/**
* 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);
*/