题目22 栈的压入、弹出

/
// 4. 题目22 栈的压入、弹出
// 输入两个整数序列,第一个整数序列表示栈的压入顺序,请判断第二个序列是否该栈的弹出序列!!!

bool StackPushPopOrder(int* piPush, int* piPop, int iLen)
{
    if (NULL == piPush || NULL == piPop || iLen <= 0)
    {
        return false;
    }

    int* piNextPush = piPush;
    int* piNextPop = piPop;
    stack<int> stStack;

    while (piNextPop - piPop < iLen)
    {
        while (stStack.empty() || stStack.top() != *piNextPop)
        {
            if (piNextPush - piPush == iLen)
            {
                break;
            }

            stStack.push(*piNextPush++);
        }

        if (stStack.top() != *piNextPop)
        {
            break;
        }

        stStack.pop();
        *piNextPop++;
    }

    if (stStack.empty() && piNextPop - piPop == iLen)
    {
        return true;
    }

    return false;
}

void StackPushPopOrderTestFunc()
{
    cout << "\n\n --------------- StackPushPopOrderTestFunc Start -------------->" << endl;
    int aiPushArray[] = {1, 2, 3, 4, 5};
    //int aiPopArray[] = {4, 3, 5, 1, 2};
    int aiPopArray[] = {5, 4, 3, 2, 1};

    int iLen = sizeof(aiPushArray) / sizeof(int);

    TRAVERSAL_ARRAY(aiPushArray, iLen);
    TRAVERSAL_ARRAY(aiPopArray, iLen);

    bool bFlag = StackPushPopOrder(aiPushArray, aiPopArray, iLen);
    if (bFlag)
    {
        cout << "TRUE" << endl;
    }
    else
    {
        cout << "FALSE" << endl;
    }


    cout << "\n\n --------------- StackPushPopOrderTestFunc End -------------->" << endl;

}

转载于:https://www.cnblogs.com/yzdai/p/11258727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值