1.用队列模拟栈
注意此处的格式要求要详细阅读
/**
* 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);
*/
这是一道学习基础题,可能面试不会碰到,但是有助于我们理解性质。
审题:
1.仅用两个队列。
2.实现类。说明方法的调用是通过调用一个整体类来实现的。
3.可以使用队列的基本操作。(C++里有队列,C原因里没有现成的队列,需要自己疯狂造轮子)
4.示例。读这个能让我们分析到自己应该怎么设计返回值。在后面出题人也对销毁格式等做出了要求。
对代码实现部分1来说几个注意的点:
1. 通过实现类来实现接口。创建一个类:返回值(一个指针) 原因:创建局部对象,出了作用域会被销毁,而且对于OJ题的结构来讲,一般更适合用返回值。创建静态对象,没法实现两个队列的折换。
2.理一下出栈的逻辑:首先队头出数据,队尾入数据。把队头的size-1个数据先入到另一个空队列(从队尾入)里,然后把最后一个数据所谓队头取出,再Pop掉。最后要返回栈顶。注意对于函数内调用有返回值的函数,注意返回值接收。
3.栈的销毁,本质上就是两个队列的销毁。再销毁对象。根据后面题的格式要求。
4.结构:我们通过这可以看到封装的思想。首先最外层是一个类,类里有两个队列。每个队列的成员有两个头尾操纵的指针,每个指针又是链表节点的基本结构单元(next指针和数据data)。其中队列的头尾两个指针的机构,也解决了单链表要传二级指针的问题。
//实现代码部分1
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
//创建
MyStack* pst=(MyStack*)malloc(sizeof(MyStack));
//初始化
QueueInit(&pst->q1);
QueueInit(&pst->q2);
return pst;
}
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);
Queue * emptyq=&obj->q1;
Queue * noemptyq=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
emptyq=&obj->q2;
noemptyq=&obj->q1;
}
//将非空队列中的size-1个导入空队列
while(QueueSize(noemptyq)>1)
{
QueuePush(emptyq,QueueFront(noemptyq));
QueuePop(noemptyq);//去除
}
int top=QueueFront(noemptyq);
//删除非空队列中剩下的最后一个数据,这个数据相当于栈顶的数据
QueuePop(noemptyq);
return top;
}
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
bool myStackEmpty(MyStack* obj) {
assert(obj);
return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
assert(obj);
QueueDestory(&obj->q1);
QueueDestory(&obj->q2);
//先释放两个结构体,
//再释放队列
free(obj);
}
复用:队列的代码部分。
//实现代码部分2
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QueueNode;
typedef struct Queue
{
QueueNode* phead;
QueueNode* ptail;
}Queue;
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->ptail == NULL && pq->phead == NULL;
}
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
}
Queue* p1;
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->phead;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->ptail == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
if (pq->phead->next == NULL)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
}
int QueueSize(Queue* pq)
{
assert(pq);
// 如果频繁调用这个接口函数,可以在Queue中给一个size记录数据个数
int n = 0;
QueueNode* cur = pq->phead;
while (cur)
{
n++;
cur = cur->next;
}
return n;
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
实现代码部分2,其实本质上就是队列的C语言实现。
1.我们回归一下逻辑:与单链表不同的是我们多了两个取头尾数据的操作。注意返回值。