forked from taohi/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22-stackPopOrder.c
More file actions
42 lines (40 loc) · 979 Bytes
/
Copy path22-stackPopOrder.c
File metadata and controls
42 lines (40 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stack>
using namespace::std;
bool isPopOrder(int *pPush ,int *pPop,int length)
{
bool result = false;
int *nextPush=pPush;
int *nextPop=pPop;
if(pPush!=NULL ||pPop!=NULL||length>0)
{
stack<int> stackData;
while(nextPop-pPop<length)
{
while(stackData.empty()||stackData.top()!=*nextPop)
{
if(nextPush-pPush==length)
break;
stackData.push(*nextPush);
nextPush++;
}
if(stackData.top()!=*nextPop)
break;
stackData.pop();
nextPop++;
}
if(stackData.empty() && nextPop-pPop==length)
result = true;
}
return result;
}
int main()
{
int push[]={1,2,3,4,5};
int pop[]={4,5,3,2,1};
if(isPopOrder(push,pop,5))
cout <<"Is Pop Order."<<endl;
else
cout <<"Not Pop Order."<<endl;
return 0;
}