题目要求
请你仅使用两个队列实现一个后入先出(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(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
思路
我们采用数组的方式,最初我们将队列的头和队列的尾指向同一个位置,通过判断相等可以确定队列为空,但是当数据存满时也是队头和队尾相等了,因此,我们需要进行区分,解决方案是在malloc空间的时候,多申请上一个空间,我们将这个空间空出来,如果rear+1和front相等那么就说明环形队列是满的。同时因为我们使用的数组,所以当front的下标、rear的下标大于数组大小的时候需要置回到0,空的位置也可以是下标0的位置。
代码实现
typedef int QDataType;
//链式结构:表示队列
typedef struct QueueNode
{
QDataType data;
struct QListNode* next;
}QueueNode;
//队列的结构
typedef struct Queue
{
QueueNode* front;
QueueNode* rear;
}Queue;
//队列的初始化
void QueueInit(Queue* pq);
//入队操作
void QueuePush(Queue* pq, QDataType data);
//出队操作
void QueuePop(Queue* pq);
//获取队列头元素
QDataType QueueFront(Queue* pq);
//获取队列尾元素
QDataType QueueBack(Queue* pq);
//获取队列中有效元素个数
int QueueSize(Queue* pq);
//队列判空
int QueueEmpty(Queue* pq);
//队列销毁
void QueueDestory(Queue* pq);
//队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->front = pq->rear = NULL;
}
//入队操作
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
newnode->data = x;
newnode->next = NULL;
if (pq->rear == NULL)
{
pq->front = pq->rear = newnode;
}
else
{
pq->rear->next = newnode;
pq->rear = newnode;
}
}
//出队操作
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
//1.只有一个节点
//2.有多个节点
if (pq->front == pq->rear)
{
free(pq->front);
pq->front = pq->rear = NULL;
}
else
{
QueueNode* next = pq->front->next;
free(pq->front);
pq->front = next;
}
}
//获取队列头元素
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->front->data;
}
//获取队列尾元素
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->rear->data;
}
//获取队列中有效元素个数
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QueueNode* cur = pq->front;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
//队列判空
int QueueEmpty(Queue* pq)
{
assert(pq);
return pq->front == NULL ? 1 : 0;
}
//队列销毁
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->front;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->front = pq->rear = NULL;
}
typedef struct {
QueueNode q1;
QueueNode q2;
} MyStack;
/** Initialize your data structure here. */
MyStack* myStackCreate() {
MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
//如果q1不为空,我们就可以在q1中进行插入
//如果q1为空,我们就默认在q2中有数据,可以在q2中进行插入,但实际情况中q2不一定有数据,比如队列本身就为空
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) {
Queue* emptyQ = &obj->q1;
Queue* nonemptyQ = &obj->q2;
//将空指针emptyQ永远指向空的那个队列
//将nonemptyQ指向非空的那个队列
if (!QueueEmpty(&obj->q1))
{
emptyQ = &obj->q2;
nonemptyQ = &obj->q1;
}
while (QueueSize(nonemptyQ) > 1)
{
QueuePush(emptyQ, QueueFront(nonemptyQ));
QueuePop(nonemptyQ);
}
//找到那个待移除的元素
int top = QueueFront(nonemptyQ);
//头删,把那个元素删除
QueuePop(nonemptyQ);
return top;
}
/** 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) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
QueueDestory(&obj->q1);
QueueDestory(&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);
*/