-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
42 lines (36 loc) · 911 Bytes
/
stack.py
File metadata and controls
42 lines (36 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
栈的存储容器可以是链表,也可以是顺序表,在这里用list来保存
"""
class Stack(object):
"""栈"""
def __init__(self):
self.__list = []
def push(self, item):
"""添加新的元素在栈顶"""
self.__list.append(item)
def pop(self):
"""弹出栈顶元素"""
return self.__list.pop()
def peek(self):
"""返回栈顶元素"""
if self.__list:
return self.__list[-1]
return None
def is_empty(self):
"""判断栈是否为空"""
return not self.__list
def size(self):
"""返回栈的元素个数"""
return len(self.__list)
if __name__ == '__main__':
s = Stack()
s.push('1')
s.push('2')
s.push('3')
s.push('4')
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())