题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
#include <stdlib.h>
int stackinTop = 0;
int stackoutTop = 0;
int stackin[1000],stackout[1000];
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param node int整型
* @return 无
*/
void push(int node ) {
// write code here
stackin[stackinTop++]=node;
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param 无
* @return int整型
*/
int pop() {
// write code here
int stackintop =stackinTop;
int stackouttop =stackoutTop;
//输出栈为空,则把输入栈所有的都放进来
if(stackouttop == 0){
while(stackintop >0){
stackout[stackouttop++]=stackin[--stackintop];
}
}
//如果不为空,就输出一个
int top = stackout[--stackouttop];
//然后把剩下的都放回输入栈
while (stackouttop > 0) {
stackin[stackintop++]=stackout[--stackouttop];
}
//记得更新指针
stackinTop = stackintop;
stackoutTop = stackouttop;
return top;
}
OPPO成长空间 952人发布