-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackPython.py
73 lines (61 loc) · 1.99 KB
/
StackPython.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
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
############################
# Author: Hemant Tripathi #
###########################
class Stack:
def __init__(self):
self.mystack = []
def isEmpty(self):
print('Is Stack Empty: ', self.mystack == [])
def push(self, element):
self.mystack.append(element);
print('Element added to stack: ',element)
def pop(self):
if(len(self.mystack) <= 0):
print(('Stack is Empty. Cannot pop element'))
else:
print('Popping element: ', self.mystack[len(self.mystack)-1])
self.mystack.pop();
def show(self):
print('Current stack is: ', self.mystack)
def showPeek(self):
print('Current Peek element is : ', self.mystack[len(self.mystack)-1])
def stackSize(self):
print('Size of Stack: ', len(self.mystack))
def main():
stack = Stack()
while(1):
print('\n\nSelect an Option:\n')
print('1. Push an Element')
print('2. Pop an Element')
print('3. Show Stack Elements')
print('4. Check if Stack is Empty')
print('5. Show Peek Element')
print('6. Stack Size')
print('7. Exit')
val = int(input()) # by default it input as a string, you need to cast it to integer
if(val == 1):
element = input('Enter the element to push: ')
stack.push(element)
continue
elif(val == 2):
print('Popping an element from stack')
stack.pop()
elif (val == 3):
print('stack elements are:')
stack.show()
elif (val == 4):
stack.isEmpty()
elif (val == 5):
print('Peek Element is:')
stack.showPeek()
elif (val == 6):
print('size of stack is:')
stack.stackSize()
elif(val == 7):
del stack
del val;
break
else:
print('Invalid input. Please enter a number between 1 to 7')
if __name__ == "__main__":
main()