No. 21 - Push and Pop Sequences ofStacks
Problem: Given two integer sequences, one ofwhich is the push sequence of a stack, please check whether the other sequenceis a corresponding pop sequence or not.
For example, if1, 2, 3, 4, 5 is a push sequence, 4, 5, 3, 2, 1 is a corresponding popsequence, but the sequence 4, 3, 5, 1, 2 is not.
Analysis: An intuitive thought on this problem isto create an auxiliary stack. We push the numbers in the first sequence one byone, and try to pop them out according to the order in the second sequence.
Take thesequence 4, 5, 3, 2, 1 as an example to analyze the process to push and pop.The first number to be popped is 4, so we need to push it into a stack. Thepushing order is defined in the first sequence, where there are numbers 1, 2and 3 prior to 4. Therefore, numbers 1, 2, and 3 are pushed into a stack before4 is pushed. At this time, there are 4 numbers in a stack, which are 1, 2, 3and 4, with 4 on top. When 4 is popped, numbers 1, 2 and 3 are left. The nextnumber to be popped is 5, which is not on top of stack, so we have to pushnumbers in the first sequence into stack until 5 is pushed. When number 5 is ontop of a stack, we can pop it. The next three numbers to be popped are 3, 2 and1. Since these numbers are on top of a stack before pop operations, they can bepopped directly. The whole process to push and pop is summarized in Table 1.
Step | Operation | Stack Status | Popped | Step | Operation | Stack Status | Popped |
1 | Push 1 | 1 | 6 | Push 5 | 1, 2, 3, 5 | ||
2 | Push 2 | 1, 2 | 7 | Pop | 1, 2, 3 | 5 | |
3 | Push 3 | 1, 2, 3 | 8 | Pop | 1, 2 | 3 | |
4 | Push 4 | 1, 2, 3, 4 | 9 | Pop | 1 | 2 | |
5 | Pop | 1, 2, 3 | 4 | 10 | Pop | 1 |
Table 1: Theprocess to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4,5, 3, 2, 1
Let us continueto analyze another pop sequence 4, 3, 5, 1, 2. The process to pop the firstnumber 4 is similar to the process above. After the number 4 is popped, 3 is onthe top of stack and it can be popped. The next number to be popped is 5. Sinceit is not on top, we have to push numbers in the first sequence until thenumber 5 is pushed. The number 5 can be popped when it is pushed onto the topof a stack. After 5 is popped out, there are only two numbers 1 and 2 left instack. The next number to be popped is 1, but it is not on the top of stack. Wehave to push numbers in the first sequence until 1 is pushed. However, allnumbers in the first sequence have been pushed. Therefore, the sequence 4, 3,5, 1, 2 is not a pop sequence of the stack with push sequence 1, 2, 3, 4, 5. Thewhole process to push and pop is summarized in Table 2.
Step | Operation | Stack Status | Popped | Step | Operation | Stack Status | Popped |
1 | Push 1 | 1 | 6 | Pop | 1, 2 | 3 | |
2 | Push 2 | 1, 2 | 7 | Push 5 | 1, 2, 5 | ||
3 | Push 3 | 1, 2, 3 | 8 | Pop | 1, 2 | 5 | |
4 | Push 4 | 1, 2, 3, 4 | The next number to be popped is 1, which is neither on the top of stack, nor in the remaining numbers of push sequence. | ||||
5 | Pop | 1, 2, 3 | 4 |
Table 1: Theprocess to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4,3, 5, 1, 2
According to theanalysis above, we get a solution to check whether a sequence is a pop sequenceof a stack or not. If the number to be popped is currently on top of stack,just pop it. If it is not on the top of stack, we have to push remainingnumbers into the auxiliary stack until we meet the number to be popped. If thenext number to be popped is not remaining in the push sequence, it is not a popsequence of a stack. The following is some sample code based on this solution:
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)
{
// When the number to be popped is not ontop of stack,
// push some numbers in the push sequenceinto stack
while(stackData.empty() || stackData.top() !=*pNextPop)
{
// If all numbers have been pushed, break
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;
}
The author HarryHe owns all the rights of this post. If you are going to use part of or thewhole of this ariticle in your blog or webpages, please add a referenceto http://codercareer.blogspot.com/. If you are going to use it in yourbooks, please contact me (zhedahht@gmail.com) . Thanks.