The above code implements the stack's IsEmpty (), Isfull (), clear (), push (), pop (), Peek () methods. The sequential stack must be checked both under overflow (underflow) and on Overflow (overflow).
Public classStack {Private int[] stack; Private Static Final intDefaultSize = 100; Private intsize; Private intTopIndex; PublicStack () {setUp (defaultsize); } PublicStack (intSZ) {setUp (SZ); } Public voidSetUp (intSZ) {Size= SZ; TopIndex = 0; stack =New int[size]; } Public BooleanIsEmpty () {returnTopIndex = = 0; } Public BooleanIsfull () {returnTopIndex = =size; } Public voidClear () {TopIndex= 0; } Public voidPushintXthrowsException {if(Isfull ())Throw NewException ("Overflow"); Elsestack[topindex++] =x; } Public intPop ()throwsException {if(IsEmpty ())Throw NewException ("Underflow"); Else returnstack[--TopIndex]; } Public intPeek ()throwsException {if(IsEmpty ())Throw NewException ("Underflow"); Else returnStack[topindex-1]; }}
Sequential stacks (array implementations)