-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_stack_using_array.py
More file actions
50 lines (41 loc) · 1.23 KB
/
implement_stack_using_array.py
File metadata and controls
50 lines (41 loc) · 1.23 KB
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
def create_array(initial_size):
arr = []
for i in range(initial_size):
arr.append(0)
return arr
class Stack:
def __init__(self, initial_size = 10):
self.arr = create_array(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 arr capacity")
self.handle_capacity()
self.arr[self.next_index] = data
self.next_index += 1
self.num_elements += 1
def handle_capacity(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
def size(self):
return len(self.arr)
def is_empty(self):
return self.num_elements == 0
def pop(self):
if self.is_empty():
self.next_index = 0
return None
last_element = self.arr[self.num_elements-1]
self.arr[self.num_elements-1] = 0
self.num_elements -= 1
self.next_index -= 1
return last_element
stack = Stack()
stack.push("a")
stack.push("b")
print(stack.pop())
print(stack.pop())
print(stack.pop())