Because of the Python weak type, it eliminates the hassle of writing generics. I define a stack class to maintain a list with the operation of the stack.
1 classStack:2 def __init__(self, _size = 1024):3Self.data = []4Self._size =_size5Self._top =-16 7 defEmpty (self):8 ifSelf._top = =-1:9 returnTrueTen Else: One returnFalse A - defsize (self): - returnSelf._top+1 the - defTop (self): - returnSelf.data[self._top] - + defClear (self): -Self.data = [] +Self._top =-1 A at defpush (self, ele): - ifSelf._top + 1 = =self._size: - Raise 'Stack over Flow' - Else: - self.data.append (ele) -Self._top + = 1 in - defpop (self): to ifself.empty (): + Raise 'Stack under Flow' - Else: theEle =Self.data[self._top] * delSelf.data[self._top] $Self._top-= 1Panax Notoginseng returnEle - thes = Stack (5) + AS.push (1) theS.push (2) +S.push (3) -S.push (5) $S.push (4) $ #S.push (233) - -Size =s.size () the forIinchRange (0, size): - Print(S.top ())WuyiS.pop ()
Python implementation Stack