示例 1:
输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示
可以先看看这一篇:用队列实现栈
好,看完题目的描述,我们来分析一下去求解这道题目
typedef struct {ST stIn;ST stOut;
} MyQueue;
MyQueue* myQueueCreate() {MyQueue* qu = (MyQueue *)malloc(sizeof(MyQueue));InitStack(&qu->stIn);InitStack(&qu->stOut);return qu;
}
PushStack(&obj->stIn, x); //入栈操作均放入stIn
return StackTop(&obj->stOut);
if(StackEmpty(&obj->stOut)) //如果stOut为空,则进行一个倒栈操作
{while(!StackEmpty(&obj->stIn)){PushStack(&obj->stOut, StackTop(&obj->stIn));PopStack(&obj->stIn);}
}
int peek = myQueuePeek(obj);
PopStack(&obj->stOut);return peek;
typedef int STDataType;
typedef struct Stack {STDataType* a;int top; //栈顶指针int capacity; //容量
}ST;/*初始化栈*/
void InitStack(ST* st);/*销毁栈*/
void DestroyStack(ST* st);/*入栈*/
void PushStack(ST* st, STDataType x);/*出栈*/
void PopStack(ST* st);/*返回栈顶元素*/
STDataType StackTop(ST* st);/*判空*/
bool StackEmpty(ST* st);/*栈的元素个数*/
int StackSize(ST* st);//--------------------------------
typedef struct {ST stIn;ST stOut;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* qu = (MyQueue *)malloc(sizeof(MyQueue));InitStack(&qu->stIn);InitStack(&qu->stOut);return qu;
}void myQueuePush(MyQueue* obj, int x) {PushStack(&obj->stIn, x); //入栈操作均放入stIn
}bool myQueueEmpty(MyQueue* obj) {return StackEmpty(&obj->stIn) && StackEmpty(&obj->stOut);
}int myQueuePop(MyQueue* obj) {int peek = myQueuePeek(obj);PopStack(&obj->stOut);return peek;
}int myQueuePeek(MyQueue* obj) {if(StackEmpty(&obj->stOut)) //如果stOut为空,则进行一个倒栈操作{while(!StackEmpty(&obj->stIn)){PushStack(&obj->stOut, StackTop(&obj->stIn));PopStack(&obj->stIn);}}return StackTop(&obj->stOut);
}void myQueueFree(MyQueue* obj) {DestroyStack(&obj->stIn);DestroyStack(&obj->stOut);free(obj);
}//--------------------------------
/*初始化栈*/
void InitStack(ST* st)
{assert(st); //警惕随意操作,传入空指针st->a = (STDataType*)malloc(sizeof(STDataType) * 4);if (st->a == NULL){perror("fail mallic");exit(-1);}st->top = 0; //初始化为0表示指向当前栈顶元素的后一元素st->capacity = 4;
}/*销毁栈*/
void DestroyStack(ST* st)
{assert(st);free(st->a);st->a = NULL;st->top = st->capacity = 0;
}/*入栈*/
void PushStack(ST* st, STDataType x)
{//栈满扩容逻辑if (st->top == st->capacity){//初始化时已经malloc开辟过空间了,因此无需考虑容量为空的情况STDataType* tmp = (STDataType*)realloc(st->a, st->capacity * 2 * sizeof(STDataType));if (tmp == NULL){perror("fail realloc");exit(-1);}st->a = tmp;st->capacity *= 2;}st->a[st->top] = x; //top指向栈顶元素的后一元素,因此直接入栈即可st->top++; //然后栈顶指针后移,为下一次入栈做准备
}/*出栈*/
void PopStack(ST* st)
{assert(st);//assert(st->top > 0);assert(!StackEmpty(st));st->top--;
}/*返回栈顶元素*/
STDataType StackTop(ST* st)
{return st->a[st->top - 1];
}/*判空*/
bool StackEmpty(ST* st)
{return (st->top == 0);
}/*栈的元素个数*/
int StackSize(ST* st)
{return st->top;
}/*** Your MyQueue struct will be instantiated and called as such:* MyQueue* obj = myQueueCreate();* myQueuePush(obj, x);* int param_2 = myQueuePop(obj);* int param_3 = myQueuePeek(obj);* bool param_4 = myQueueEmpty(obj);* myQueueFree(obj);
*/
以上就是本文所要描述的所有内容,感谢您对本文的观看,如有疑问请于评论区留言或者私信我都可以🍀