Title 22 of the stack push, pop

////////////////////////////////////////////////// ///////////////////////////////////
// 22 4. title stack push, pop
// input two sequence of integers, the first integer sequence into a sequence of press-stack, pop-up sequence to check the stack determines the second sequence! ! !

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;

}

Guess you like

Origin www.cnblogs.com/yzdai/p/11258727.html