22栈的压入,弹出序列

来源:互联网 发布:淘宝注册怎么注册账号 编辑:程序博客网 时间:2024/06/07 05:09



bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
{
bool bPossible = false;
if (pPush != NULL&&pPop != NULL&&nLength > 0)
{
const int* pNextPush = pPush;
const int* pNextPop = pPop;
std::stack<int> stackData;
while (pNextPop - pPop < nLength)
{
while (stackData.empty() || stackData.top() != *pNextPop)
{
if (pNextPush - pPush == nLength)
break;//判断是否第一个序列全都压入栈
stackData.push(*pNextPush);
pNextPush++;
}
if (stackData.top() != *pNextPop)
break;
stackData.pop();
pNextPop++;
}
if (stackData.empty() && pNextPop - pPop == nLength)//第二个序列中还有元素
bPossible = true;
}
return bPossible;
}

0 0
原创粉丝点击