首页 > 其他 > 详细

题目22 栈的压入、弹出

时间:2019-07-28 15:54:02      阅读:88      评论:0      收藏:0      [点我收藏+]

/////////////////////////////////////////////////////////////////////////////////////
// 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;

}

题目22 栈的压入、弹出

原文:https://www.cnblogs.com/yzdai/p/11258727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!