push(object)
object pop()
object top()
integer len()
boolean is_empty()
EX. 웹 브라우저 뒤로가기, TEXT Editor Undo 액션, 파이썬의 activation record
O(n)
O(1)
class ArrayStack:
def __init__(self):
self._data = []
def __len__(self):
return len(self._data)
def is_empty(self):
return len(self._data) == 0
def push(self, e):
self._data.append(e)
def top(self):
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1]
def pop(self):
if self.is_empty():
raise Empty('Stack is empty')
return self._data.pop()
def is_matched(expr):
lefty = '({['
righty = ')}]'
S = ArrayStack()
for c in expr:
if c in lefty:
S.push(c)
elif c in righty:
if S.is_empty():
return False
if righty.index(c) != lefty.index(S.pop()):
return False
return S.is_empty()
14 - 3 * 2 + 7 = ( 14 - (3*2) ) + 7
prec
우선 순위$