-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_using_python_list.py
More file actions
35 lines (28 loc) · 959 Bytes
/
Copy pathStack_using_python_list.py
File metadata and controls
35 lines (28 loc) · 959 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
class Stack:
def __init__(self, initial_size = 10):
self.arr = [0 for _ in range(initial_size)]
self.next_index = 0
self.num_elements = 0
def push(self, data):
if self.next_index == len(self.arr):
print("Out of space! Increasing array capacity ...")
self._handle_stack_capacity_full()
self.arr[self.next_index] = data
self.next_index += 1
self.num_elements += 1
# the pop method
def pop(self):
if self.num_elements==0:
return None
self.next_index-=1
self.num_elements-=1
return "popped"
def size(self):
return self.num_elements
def is_empty(self):
return self.num_elements == 0
def _handle_stack_capacity_full(self):
old_arr = self.arr
self.arr = [0 for _ in range( 2* len(old_arr))]
for index, value in enumerate(old_arr):
self.arr[index] = value