题解 | #用两个栈实现队列#

用两个栈实现队列

https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param node int整型 
 * @return 无
 */
 static int instack[1000];
 static int outstack[1000];
 static int top1=0;
 static int top2=0;
 void push(int node ) {
    // write code here
    instack[top1++]=node;
}

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param 无 
 * @return int整型
 */
 
int pop() {
    // write code here
    if (top2==0) 
    {
        top1--;
        while(top1>=0)
    {
        outstack[top2++]=instack[top1--];
    }
    top1++;
    return outstack[--top2];
    }
    else {
    return outstack[--top2];
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务