栈的压入、弹出序列

bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
{
	if (pPush == nullptr || pPop == nullptr || nLength <= 0)
		return false;

	std::stack<int> stackData;
	const int* pNextPush = pPush;
	const int* pNextPop = pPop;
	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)
		return true;
	else
		return false;
}

  

posted on 2021-02-18 14:21  Noora&w  阅读(52)  评论(0编辑  收藏  举报