标签:raise false pac stack += 数组 coding 元素 append
1.栈,堆栈,先进后出
2.栈的几个操作:
> 入栈,push
> 出栈,pop
> 获取栈顶元素,peek
> 获取栈中共有元素个数,getSize
> 是否为空,isEmpty
#coding:utf-8 class Stack: """ 自定义栈 """ _arr = [] capacity = 0 size = 0 def __init__(self, capacity=10): self.capacity = capacity def push(self, element): self._arr.append(element) self.size += 1 def pop(self): if self.size <= 0: raise Exception("Stack is empty.") del self._arr[self.size - 1] return self.size - 1 def peek(self): pass def get_size(self): pass def is_empty(self): if self.size > 0: return False return True def __str__(self): content = ["Stack :"] for arr in self._arr: content.append(str(arr)) return "".join(content) if __name__ == ‘__main__‘: stack = Stack() stack.push("hi") stack.push("hello") print(stack) stack.pop() print(stack.is_empty()) print(stack)
标签:raise false pac stack += 数组 coding 元素 append
原文地址:https://www.cnblogs.com/n-n5980/p/11870605.html