In practice a RAM is used to simulate the operation of a sta
In practice, a RAM is used to simulate the operation of a stack by manipulating the address value in the MAR (memory address register). Here, the data values in the stack do not actually shift during PUSH and POP operations. Rather, the address of the RAM location corresponding to the data input and output is changed appropriately. Determine the sequence of operations needed for PUSH and POP.
Solution
For example, if 1, 2, 3, 4, 5 is a push sequence, 4, 5, 3, 2, 1 is a corresponding pop sequence, but the sequence 4, 3, 5, 1, 2 is not.
Analysis: An intuitive thought on this problem is to create an auxiliary stack. We push the numbers in the first sequence one by one, and try to pop them out according to the order in the second sequence.
Take the sequence 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. The pushing order is defined in the first sequence, where there are numbers 1, 2 and 3 prior to 4. Therefore, numbers 1, 2, and 3 are pushed into a stack before 4 is pushed. At this time, there are 4 numbers in a stack, which are 1, 2, 3 and 4, with 4 on top. When 4 is popped, numbers 1, 2 and 3 are left. The next number to be popped is 5, which is not on top of stack, so we have to push numbers in the first sequence into stack until 5 is pushed. When number 5 is on top of a stack, we can pop it. The next three numbers to be popped are 3, 2 and 1. Since these numbers are on top of a stack before pop operations, they can be popped 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: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 5, 3, 2, 1
Let us continue to analyze another pop sequence 4, 3, 5, 1, 2. The process to pop the first number 4 is similar to the process above. After the number 4 is popped, 3 is on the top of stack and it can be popped. The next number to be popped is 5. Since it is not on top, we have to push numbers in the first sequence until the number 5 is pushed. The number 5 can be popped when it is pushed onto the top of a stack. After 5 is popped out, there are only two numbers 1 and 2 left in stack. The next number to be popped is 1, but it is not on the top of stack. We have to push numbers in the first sequence until 1 is pushed. However, all numbers 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. The whole 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: The process to push and pop with a push sequence 1, 2, 3, 4, 5 and pop sequence 4, 3, 5, 1, 2
According to the analysis above, we get a solution to check whether a sequence is a pop sequence of 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 remaining numbers into the auxiliary stack until we meet the number to be popped. If the next number to be popped is not remaining in the push sequence, it is not a pop sequence 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 on top of stack,
// push some numbers in the push sequence into 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;
}
| 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 |




