forked from geekquad/AlgoBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.py
41 lines (32 loc) · 799 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
"""
Time Complexity
---------------
push O(1)
pop O(1)
is_empty O(1)
is_full O(1)
peek O(1)
size O(1)
"""
class Stack(object):
def __init__(self, capacity=5):
self.stack = []
self.capacity = capacity
def is_empty(self):
return len(self.stack) <= 0
def is_full(self):
return len(self.stack) >= self.capacity
def push(self, item):
if self.is_full():
print("Stack Overflow!")
else:
self.stack.insert(0, item)
def pop(self):
if self.is_empty():
print("Stack Underflow")
else:
self.stack.pop(0)
def peek(self):
return self.stack[0]
def size(self):
return len(self.stack)