Enter Two integer sequences. the first sequence indicates the order in which the stack is pushed. Determine whether the second sequence is in the pop-up order of the stack. Assume that all numbers pushed into the stack are not equal.
Code:
# Include <iostream> # include <stack> # include <assert. h> using namespace STD; bool isstackseq (int * ppush, int * ppop, int length) {bool isstack = false; If (ppush! = NULL & ppop! = NULL & length> 0) {constint * ppushnext = ppush; const int * ppopnext = ppop; stack <int> stackdata; while (ppopnext-ppop <length) {// import elements into the stack while (stackdata. empty () | stackdata. top ()! = * Ppopnext) {If (ppushnext-ppush = length) break; stackdata. Push (* ppushnext); ppushnext ++;} If (stackdata. Top ()! = * Ppopnext) break; stackdata. pop (); ppopnext ++;} If (stackdata. empty () & ppushnext-ppush = length) isstack = true;} return isstack;} int main () {int data1 [] = {1, 2, 3, 4, 5 }; int data2 [] = {4, 5, 3, 2, 1}; cout <isstackseq (data1, data2, sizeof (data1)/sizeof (INT); Return 0 ;}
Running result: