Loading...
1procedure PUSH(S, x)2 top ← top + 13 S[top] ← x4end procedure
1procedure POP(S)2 if top < 0 then3 error "UNDERFLOW"4 end if5 x ← S[top]6 top ← top − 17 return x8end procedure
1procedure PEEK(S)2 if top < 0 then3 error "EMPTY"4 end if5 return S[top]6end procedure
1procedure IS_BALANCED(expr)2 create empty stack S3 for each ch in expr do4 if ch in '(', '[', '{' then5 push(S, ch)6 else if ch in ')', ']', '}' then7 if S is empty then return false8 open ← pop(S)9 if NOT matches(open, ch) then return false10 end if11 end for12 return S is empty13end procedure