C ++: Check the legitimacy of the stack Sequence
CheckSequence. cpp # include <iostream> # include <assert. h> using namespace std; # include <stack> bool Checksequence (int * stackIn, int * stackOut, int lenIn, int lenOut) {assert (stackIn & stackOut ); if (lenIn! = LenOut) // return false if the two sequence lengths are not equal; stack <int> s; for (int I = 0; I <lenIn; I ++) {int j = 0; s. push (stackIn [I]); while (s. size ()> 0 & s. top () = stackOut [j]) {// The top element of the inbound stack sequence is not the same as that of the current outbound stack sequence. It is invalid. pop (); j ++;} return (s. size ()> 0 )? False: true; // the stack is not empty or valid after all the out-of-stack sequence elements are matched.} int main () {int stackIn [] = {1, 2, 3, 4, 5}; // The inbound stack sequence int stackOut [] = {5, 4, 2, 3, 1 }; // output stack sequence int len_in = sizeof (stackIn)/sizeof (stackIn [0]); // The length of the inbound stack sequence int len_out = sizeof (stackOut) /sizeof (stackOut [0]); // length of the output stack sequence bool ret = Checksequence (stackIn, stackOut, len_in, len_out); if (ret) cout <"valid stack order" <endl; else cout <"invalid stack order" <endl; return 0 ;}