




#ifndef STACK_PUSH_POP_SEQ_H#define STACK_PUSH_POP_SEQ_H#include<iostream>#include<stack>bool stackPPseq(const int *pPussh,const int *pPop,int nLength){int pushIndex=0;int popIndex=0;std::stack<int> m_stack;if(pPussh==NULL||pPop==NULL||nLength==0){return false;}while(pushIndex<nLength&&popIndex<nLength){if(m_stack.empty()){ //如果栈为空,则一直从题目给的压栈顺序数组中提取元素压入栈中,直到碰到一个与弹出数组相等的元素。while(pPussh[pushIndex]!=pPop[popIndex]){m_stack.push(pPussh[pushIndex]);++pushIndex;}m_stack.push(pPussh[pushIndex]);}if(m_stack.top()==pPop[popIndex]){ //如果弹出栈的数组元素与栈顶的元素相等,则一直弹。while(m_stack.top()==pPop[popIndex]){m_stack.pop();popIndex++;if(popIndex==nLength){return true;}}}if(m_stack.top()!=pPop[popIndex]){//如果栈顶没有与弹出数组元素相等的,则再压。while(m_stack.top()!=pPop[popIndex]){pushIndex++;if(pushIndex==nLength){break;}m_stack.push(pPussh[pushIndex]);}}}if(pPop[popIndex]==m_stack.top()){m_stack.pop();popIndex++;if(popIndex>=nLength){return true;}}return false;}#endif
原文:http://www.cnblogs.com/yml435/p/4655486.html