文章目录
- 一、双队列实现栈
- 二、双栈实现队列
一、双队列实现栈
题目链接:
https://leetcode.cn/problems/implement-stack-using-queues/description/
题目分析: 栈的结构是后进先出,而队列的结构是先进先出,我们利用这个性质,可以把一个队列用于存数据,一个队列用于倒数据
//把之前队列的实现函数摆出来
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;}
void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* cur = pq->phead;while (cur){cur = pq->phead->next;free(pq->phead);pq->phead = cur;}pq->phead = pq->ptail = NULL;pq->size = 0;
}
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail");return;}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(Queue* pq)
{assert(pq);assert(pq->phead!=NULL);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;}pq->size--;}
QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;}
QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
题目要求我们要写这几个函数
typedef struct {Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() {//先造一个栈MyStack* obj = (MyStack*)malloc(sizeof(MyStack));//初始化队列QueueInit(&obj->q1);QueueInit(&obj->q2);return obj;
}void myStackPush(MyStack* obj, int x) {if(!QueueEmpty(&obj->q1)){QueuePush(&obj->q1,x);}else{QueuePush(&obj->q2,x);}}int myStackPop(MyStack* obj) {
//假设法判断哪个队列为空Queue* Empty = &obj->q1;Queue* NonEmpty = &obj->q2;if(!QueueEmpty(&obj->q1)){Empty = &obj->q2;NonEmpty = &obj->q1;}while(QueueSize(NonEmpty)>1){QueuePush(Empty,QueueFront(NonEmpty));QueuePop(NonEmpty);}int end = QueueFront(NonEmpty);QueuePop(NonEmpty);return end;
}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);
}
二、双栈实现队列
题目链接:
https://leetcode.cn/problems/implement-queue-using-stacks/submissions/506480576/
void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = ps->top = 0;}void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}void StackPush(ST* ps, STDataType x)
{assert(ps);
if (ps->capacity == ps->top)
{int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)*newcapacity);if (tmp == NULL){perror("realloc fail");return;}ps->a = tmp;ps->capacity = newcapacity;
}ps->a[ps->top] = x;ps->top++;}void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));ps->top--;
}STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}int StackSize(ST* ps)
{assert(ps);return ps->top;
}bool StackEmpty(ST* ps)
{assert(ps);return ps->top==0;
}
题目要求写的函数:
typedef struct {ST pushst;ST popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));StackInit(&obj->pushst);StackInit(&obj->popst);return obj;
}void myQueuePush(MyQueue* obj, int x) {assert(obj);assert(&obj->pushst);StackPush(&obj->pushst,x);
}int myQueuePop(MyQueue* obj) {int end = myQueuePeek(obj);StackPop(&obj->popst);return end;
}//栈和队列的性质不一样,栈倒过来就是反的,而队列倒过来还是正的
int myQueuePeek(MyQueue* obj) {assert(obj);assert(&obj->pushst);if(StackEmpty(&obj->popst)){while(!StackEmpty(&obj->pushst)){StackPush(&obj->popst,StackTop(&obj->pushst));StackPop(&obj->pushst);}}return StackTop(&obj->popst);
}bool myQueueEmpty(MyQueue* obj) {return StackEmpty(&obj->popst)&&StackEmpty(&obj->pushst);
}void myQueueFree(MyQueue* obj) {StackDestroy(&obj->popst);StackDestroy(&obj->pushst);free(obj);
}