题目:输入两个整数序列,第一个序列表示站的压入顺序,请判断第二个栈是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。
#include "iostream"
#include "stack"
using namespace std;
void IsPopOrder(const int* pPush,const int* pPop)
{
stack<int> stack1;
const int* p1 =pPush;
const int* p2 =pPop;
stack1.push(*(p1++));
while(*p1!=NULL&&!stack1.empty())
{
if (*p2 ==stack1.top())
{
stack1.pop();
p2++;
}
else
{
stack1.push(*(p1++));
}
}
if (!stack1.empty())
{
cout<<"第二个顺序不是一个出栈顺序!";
return;
}
if(*p1 ==NULL)
{
while(*p2 ==stack1.top())
{
stack1.pop();
p2++;
}
if (*p2 !=NULL)
{
cout<<"第二个顺序不是一个出栈顺序!";
return;
}
}
cout<<"第二个是第一个的一个出栈顺序!\n";
}
void main()
{
int input1[] ={1,2,3,4,5};
int input2[] ={4,5,3,2,1};
int input3[] ={4,3,5,1,2};
IsPopOrder(input1,input2);
IsPopOrder(input1,input3);
cout<<endl;
}