其他分享
首页 > 其他分享> > 题目22 栈的压入、弹出

题目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;

}

标签:piNextPop,题目,压入,22,int,iLen,piPop,stStack
来源: https://www.cnblogs.com/yzdai/p/11258727.html