1.棧,堆棧,先進後出app
2.棧的幾個操做:spa
> 入棧,pushcode
> 出棧,popblog
> 獲取棧頂元素,peekutf-8
> 獲取棧中共有元素個數,getSizeci
> 是否爲空,isEmptyelement
#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)