class StackEmptyError(Exception): pass class StackFullError(Exception): pass class Stack: def __init__(self, size): self.index = 0 self.size = size self.lst = [] def pop(self): if self.index > 0: ret = self.lst[self.index] return ret else: raise StackEmptyError("stack has already empty") def push(self, el): if self.index > self.size: raise StackFullError("stack is full") else: self.lst[self.index] = el self.index = self.index + 1 def clear(self): self.lst.clear() self.index = 0 def __sizeof__(self): return len(self.lst) def max(self): return self.size def now(self): return self.index
原文地址:https://www.cnblogs.com/tcpblog/p/9755582.html
时间: 2024-11-12 01:02:53