#include "stdio.h"
#include "stdbool.h"
#include "string.h"
#include "stdlib.h"
#include "assert.h"
//初始化队列
typedef int QueueDataType;
typedef struct queue {
QueueDataType val;
struct queue* next;
}Qnode;
typedef struct Queue {
Qnode* phead;//队头
Qnode* ptail;//队尾
int size;//队列中的有效数据
}Queue;
//初始化队
void QueueInit(Queue* pq);
//入队
void QueuePush(Queue* pq,QueueDataType x);
//出队
void QueuePop(Queue* pq);
//获取队首元素
QueueDataType QueueFront(Queue* pq);
//获取队尾元素
QueueDataType QueueBack(Queue* pq);
//获取队列有效个数
int QueueSize(Queue* pq);
//判断队列是否为空,为空返回1,非空返回0
bool QueueEmpty(Queue* pq);
//打印队列
void QueuePrint(Queue* pq);
//销毁队列
void QueueDestory(Queue* pq);
#define _CRT_SECURE_NO_WARNINGS 1
//初始化队
void QueueInit(Queue* pq)
{
assert(pq);
pq->size=0;
pq->phead=pq->ptail=NULL;
}
//入队
void QueuePush(Queue* pq, QueueDataType x)
{
assert(pq);
assert(pq);
Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
pq->size++;
newnode->val = x;
newnode->next = NULL;
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
pq->ptail->next = newnode;
pq->ptail = newnode;
}
}
//出队
void QueuePop(Queue* pq) {
assert(pq);
Qnode* next = pq->phead->next;
free(pq->phead);
pq->size--;
pq->phead = next;
}
//获取队首元素
QueueDataType QueueFront(Queue* pq) {
assert(pq);
assert(pq->size > 0);
assert(pq->phead);
return pq->phead->val;
}
//获取队尾元素
QueueDataType QueueBack(Queue* pq) {
assert(pq);
assert(pq->ptail);
assert(pq->size > 0);
return pq->ptail->val;
}
//获取队列有效个数
int QueueSize(Queue* pq) {
assert(pq);
return pq->size;
}
//判断队列是否为空,为空返回1,非空返回0
bool QueueEmpty(Queue* pq) {
assert(pq);
return pq->size == 0 ? 1 : 0;
}
//打印队列
void QueuePrint(Queue* pq) {
assert(pq);
assert(pq->phead);
while (!QueueEmpty(pq))
{
printf("%d\n", pq->phead->val);
QueuePop(pq);
}
}
//销毁队列
void QueueDestory(Queue* pq) {
assert(pq);
Qnode* cur = pq->phead;
while (cur) {
Qnode* next = cur->next;
free(cur);
cur = next;
}
pq->phead = pq->ptail = NULL;
}
typedef struct {
Queue pq1;
Queue pq2;
} MyStack;
MyStack* myStackCreate() {
MyStack* ST=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&ST->pq1);
QueueInit(&ST->pq2);
return ST;
}
void myStackPush(MyStack* obj, int x) {
if(QueueEmpty(&obj->pq1))
{
QueuePush(&obj->pq2,x);
}else{
QueuePush(&obj->pq1,x);
}
}
int myStackPop(MyStack* obj) {
if(QueueEmpty(&obj->pq1)){
while((&obj->pq2)->phead!=(&obj->pq2)->ptail)
{
QueuePush(&obj->pq1,QueueFront(&obj->pq2));
QueuePop(&obj->pq2);
}
int vala=QueueFront(&obj->pq2);
QueuePop(&obj->pq2);
return vala;
}
if(QueueEmpty(&obj->pq2)){
while((&obj->pq1)->phead!=(&obj->pq1)->ptail)
{
QueuePush(&obj->pq2,QueueFront(&obj->pq1));
QueuePop(&obj->pq1);
}
}
int valb=QueueFront(&obj->pq1);
QueuePop(&obj->pq1);
return valb;
}
int myStackTop(MyStack* obj) {
if(QueueEmpty(&obj->pq1)){
return (&obj->pq2)->ptail->val;
}
return (&obj->pq1)->ptail->val;
}
bool myStackEmpty(MyStack* obj) {
if(QueueEmpty(&obj->pq1)&&QueueEmpty(&obj->pq2)){
return true;
}
return false;
}
void myStackFree(MyStack* obj) {
Qnode* cur1 = (&obj->pq1)->phead;
while(cur1){
Qnode* next=cur1->next;
free(cur1);
cur1=next;
}
(&obj->pq1)->phead=(&obj->pq1)->ptail=NULL;
Qnode* cur2 = (&obj->pq2)->phead;
while(cur2){
Qnode* next=cur2->next;
free(cur2);
cur2=next;
}
(&obj->pq2)->phead=(&obj->pq2)->ptail=NULL;
(&obj->pq2)->phead=(&obj->pq2)->ptail=NULL;
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);
*/