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

用两个栈实现队列

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

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * @author Senky
 * @date 2023.08.24
 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1
 * @brief 两个栈
 *          入栈时,入栈1,
 *          出栈时,栈2空,则记录当前栈1元素的入栈逆序,出栈
 *                 栈2不空,则出栈
 */
#include <stdlib.h>
//栈和各自的栈顶指针
int stack_push[1001] = {0,};
int stack_pull[1001] = {0,};

int stack_push_top = 0;
int stack_pull_top = 0;


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

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param 无 
 * @return int整型
 */
int pop() 
{
    // write code here
    //将stack_push全部压入stack_pull
    if(0 == stack_pull_top)
    {
        while (stack_push_top != 0)
        {
            stack_pull[stack_pull_top++] = stack_push[--stack_push_top];
        }
    }

    return stack_pull[--stack_pull_top];
    //出栈
    
}

#TOP101#
TOP101-BM系列 文章被收录于专栏

系列的题解

全部评论

相关推荐

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