-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks_class.py
More file actions
79 lines (64 loc) · 1.74 KB
/
stacks_class.py
File metadata and controls
79 lines (64 loc) · 1.74 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
71
72
73
74
75
76
77
78
79
# Class version of stack
# LIFO: Last in First Out
class Stack:
def __init__(self):
self._elements = []
def push(self, item):
self._elements.append(item)
def pop(self):
if len(self._elements) > 0:
return self._elements.pop()
else:
return None
def peek(self):
if len(self._elements) > 0:
return self._elements[ len(self._elements)-1]
else:
return None
def clear(self):
self._elements.clear()
def __str__(self) -> str:
return f"STACK: {self._elements}"
def main():
stack = Stack()
while True:
print("""
Stacks in python.
Sample script for demostrate how to Stacks works.
LIFO: Last In First Out
Operations:
1 Push
2 Pop
3 Peek
4 Clear
5 View
6 EXIT
""")
option = input("Enter your option: ")
if option.isdigit():
option = int(option)
if option == 1:
print("\n PUSH item:")
item = input("Enter the item: ")
stack.push(item)
elif option == 2:
print("\n POP item:")
el = stack.pop()
print(el)
elif option == 3:
print("\n PEEK item:")
el = stack.peek()
print(el)
elif option == 4:
print("\n Clear stack:")
stack.clear()
elif option == 5:
print("\n Stack Members:")
print(stack)
else:
break
else:
print("Incorrect option")
break
if __name__ == "__main__":
main()