Introduction to algorithms / Thomas H. Cormen...[etal.].—3rded.

 

 

If we attempt to pop an empty stack, we say the stack underflows, which is normally an error.If S.top exceeds n, the stack overflows.
 

 

STACK-EMPTY(S)
if S.top == 0
    return TRUE
else return FALSE

PUSH(S, x)
S.top = S.top + 1
S[S.top] = x

POP(S)
if STACK-EMPTY(S)
    error 'underflow'
else S.top = S.top - 1
    return S[S.top + 1]

 

//we can implement  a stack of at mostnelements withan arraySŒ1::n. 
DIY-FULL(S)
if S.top < n
    return FLASE
else return TRUE

DIY-POP(S)
if STACK-EMPTY(S)
    error 'underflow'
else if DIY-FULL(S)
    error 'overflow'
else S.top = S.top - 1
     return S[S.top + 1]