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]

 

相关文章:

  • 2022-03-03
  • 2021-08-15
  • 2021-07-12
  • 2021-09-24
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-03
  • 2021-09-25
  • 2021-12-22
  • 2021-08-18
  • 2022-12-23
相关资源
相似解决方案