// Header file # Include <Stdio. h> # Include <Malloc. h> # Define Maxsize 100 Typedef Char Elemtype; typedef Struct {Elemtype data [maxsize]; Int Top; /* Stack pointer */ } Sqstack; /* Sequence stack Type Definition */ Void Initstack (sqstack *& S) {s = (Sqstack *) malloc ( Sizeof (Sqstack); s -> Top =- 1 ;} Void Clearstack (sqstack *& S) {free (s );} Int Stacklength (sqstack *S ){ Return (S-> top + 1 );} Int Stackempty (sqstack * S ){ Return (S-> Top =- 1 );} Int Push (sqstack *& S, elemtype e ){ If (S-> Top = maxsize- 1 ) /* Stack Overflow */ Return 0 ; S -> Top ++ ; S -> Data [S-> top] = E; Return 1 ;} Int Pop (sqstack * & S, elemtype & E ){ If (S-> Top =- 1 )/* Stack is empty, that is, off-Stack Overflow */ Return 0 ; E = S-> data [S-> Top]; S -> Top -- ; Return 1 ;} Int Gettop (sqstack * s, elemtype & E ){ If (S-> Top =- 1 ) /* Stack is empty, that is, off-Stack Overflow */ Return 0 ; E = S-> data [S-> Top]; Return 1 ;} Void Dispstack (sqstack * S ){ Int I; For (I = s-> top; I> =0 ; I -- ) Printf ( " % C " , S-> Data [I]); printf ( " \ N " );} // Implementation Functions # Include <Iostream> # Include " Heshan1.h " Using Namespace STD; Int Judge ( Char A []) { Int I; elemtype X; sqstack * S; initstack (s ); For (I = 0 ; A [I]! = ' \ 0 ' ; I ++){ If (A [I] = ' I ' ) Push (s, A [I]); Else If (A [I] = ' O ' ){ If (S-> Top =- 1 ) // When an "O" operation is performed, the system first checks whether the stack is empty. Otherwise, the operation continues. { Return 0 ;} Else Pop (S, x );}} Return (S-> Top =- 1 );} Void Main (){ Char A [ 20 ]; Int I; For (I = 0 ; I < 20 ; I ++ ) {A [I] = ' \ 0 ' ;} Cout < " Enter operation sequence (I, O sequence) " < Endl; CIN >A; Int M = Judge (); If (M = 0 ) Cout < " The operation sequence you entered is invalid. " < Endl; Else Cout < " The operation sequence you entered is legal " <Endl ;}