-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.py
54 lines (42 loc) · 962 Bytes
/
Stack.py
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
43
44
45
46
47
48
49
50
51
52
53
class Stack:
def __init__(self):
self.items = []
def push(self, item):
"""
push an item into the stack
:param item:
:return:
"""
self.items.append(item)
def pop(self):
"""
Removes the top item in the stack
:return: top item in the stack
"""
if self.items:
return self.items.pop()
return None
def peek(self):
if self.items:
return self.items[-1]
return None
def size(self):
"""
:return: Size of the stack
O(1)
"""
return len(self.items)
def is_empty(self):
"""
:return: True if stack is empty
"""
if self.items:
return True
return False
my_stack = Stack()
my_stack.pop()
my_stack.push('apple')
my_stack.push('banana')
print(my_stack.items)
my_stack.pop()
print("After pop: ",my_stack.items)