-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_stack_array_oop.py
More file actions
70 lines (58 loc) · 1.64 KB
/
static_stack_array_oop.py
File metadata and controls
70 lines (58 loc) · 1.64 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Stack:
def __init__(self, max_size):
self.max_size = max_size
self.stack = [None] * max_size
self.top = -1
def is_empty(self):
return self.top == -1
def is_full(self):
return self.top == self.max_size-1
def push(self, item):
if self.is_full():
print("stack full. cannot add.")
else:
self.top += 1
self.stack[self.top] = item
def pop(self):
if self.is_empty():
return "stack empty. cannot pop."
else:
self.top -= 1
return self.stack[self.top+1]
def peek(self):
if self.is_empty():
return "stack empty. cannot peek."
else:
return self.stack[self.top]
def display(self):
if self.is_empty():
return "stack empty"
else:
return self.stack[:self.top+1]
def display_raw_array(self):
print(self.stack)
# Main
new_stack = Stack(3)
print("Display:", new_stack.display())
print()
new_stack.push("apple")
print("Display:", new_stack.display())
print("Peek: ", new_stack.peek())
print()
print("Popped item:", new_stack.pop())
print("Display:", new_stack.display())
print("Peek: ", new_stack.peek())
print()
print("Popped item:", new_stack.pop())
print("Display:", new_stack.display())
new_stack.display_raw_array()
print()
new_stack.push("pear")
new_stack.push("grapes")
new_stack.push("orange")
new_stack.push("berry")
print("Display:", new_stack.display())
print("Popped item:", new_stack.pop())
print("Display:", new_stack.display())
print("Peek: ", new_stack.peek())
new_stack.display_raw_array()