请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
思路:
(1)把PushST栈中的size-1个元素倒入PopST栈中,将最后一个1出栈;
(2)再把PopST栈中的元素依次出栈;
(3)若还要进栈,则进入PushST栈中,再利用(1)、(2)方法进行出栈顺序。
//思路:
//(1)把PushST栈中的size-1个元素倒入PopST栈中,将最后一个1出栈;
//(2)再把PopST栈中的元素依次出栈;
//(3)若还要进栈,则进入PushST栈中,再利用(1)、(2)方法进行出栈顺序。
typedef int STDataType;
struct Stack
{
STDataType* a; //数组
int top; //栈顶
int capacity; //容量,方便增容
};
typedef struct Stack Stack;
void StackInit(Stack* pst);
void StackDestroy(Stack* pst);
void StackPush(Stack* pst, STDataType x);
void StackPop(Stack* pst);
STDataType StackTop(Stack* pst);
//空返回1,非空返回0
bool StackEmpty(Stack* pst);
int StackSize(Stack* pst);
//初始化栈
void StackInit(Stack* pst)
{
assert(pst);
//不方便增容
/*pst->a = NULL;
pst->top = 0;
pst->capacity = 0;*/
pst->a = (STDataType*)malloc(sizeof(STDataType)* 4);
pst->top = 0;
pst->capacity = 4;
}
//销毁栈
void StackDestroy(Stack* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->capacity = pst->top = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
STDataType* tmp =(STDataType*) realloc(pst->a,sizeof(STDataType)*pst->capacity * 2);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1); //结束整个程序
}
pst->a = tmp;
pst->capacity *= 2;
}
//入数据
pst->a[pst->top] = x;
pst->top++;
}
//出栈
void StackPop(Stack* pst)
{
assert(pst);
assert(!StackEmpty(pst));
pst->top--;
}
STDataType StackTop(Stack* pst)
{
assert(pst);
assert(!StackEmpty(pst));
return pst->a[pst->top - 1];
}
bool StackEmpty(Stack* pst)
{
assert(pst);
return pst->top == 0;
}
int StackSize(Stack* pst)
{
assert(pst);
return pst->top;
}
//在队列中定义两个所需的栈
typedef struct {
Stack pushST;
Stack popST;
} MyQueue;
/** Initialize your data structure here. */
MyQueue* myQueueCreate() {
MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&q->pushST);
StackInit(&q->popST);
return q;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
//将数据进入栈
StackPush(&obj->pushST,x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
if(StackEmpty(&obj->popST))
{
while(!StackEmpty(&obj->pushST))
{
StackPush(&obj->popST,StackTop(&obj->pushST)); //将栈中有元素的栈中元素倒入到另一个栈中
StackPop(&obj->pushST); //把栈中有元素的栈的最后一个元素出栈
}
}
int top = StackTop(&obj->popST); //依次从PopST栈中出数据
StackPop(&obj->popST); //返回出栈数据值
return top;
}
/** Get the front element. */
//获取队头数据
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->popST))
{
while(!StackEmpty(&obj->pushST))
{
StackPush(&obj->popST,StackTop(&obj->pushST)); //将栈中有元素的栈中元素倒入到另一个栈中
StackPop(&obj->pushST); //把栈中有元素的栈的最后一个元素出栈
}
}
return StackTop(&obj->popST); //获取栈数据元素
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
//只要有一个不为空都不算为空
return StackEmpty(&obj->pushST) && StackEmpty(&obj->popST);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->pushST);
StackDestroy(&obj->popST);
free(obj);
}