[数据结构] 栈的列表实现

用Python内置的列表(list)实现栈,代码如下:

 1 import os
 2 os.chdir("E:\Python_temp")
 3 
 4 class Stack:
 5     def __init__(self):
 6         self._a = []
 7     def __len__(self):
 8         return len(self._a)
 9     def isEmpty(self):
10         return len(self._a) == 0 
11     def push(self,item):
12         self._a +=[item]
13     def pop(self):
14         self._a.pop()
15         
16 def main():
17     stack = Stack()
18     stack.push("a")
19     stack.push("b")
20     stack.push("c")
21     stack.pop()
22     for item in stack._a:
23         print(item)
24         
25 if __name__ == "__main__": main()

输出结果:
>> a
>> b

 

原文地址:https://www.cnblogs.com/cxc1357/p/10295105.html