-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
40 lines (32 loc) · 859 Bytes
/
stack.py
File metadata and controls
40 lines (32 loc) · 859 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
class Stack(object):
'''Stack'''
def __init__(self):
self.__list = []
def push(self, item):
'''Add a new item to the top of the stack'''
self.__list.append(item)
def pop(self):
'''Remove the item at the top of the stack'''
return self.__list.pop()
def peek(self):
'''Return the item at the top of the stack'''
if self.__list:
return self.__list[-1]
else:
return None
def is_empty(self):
'''Determine if the stack is empty'''
return self.__list == []
def size(self):
'''Return the number of the items '''
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())