-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMenu_Driven_Code_for_Stack.py
95 lines (76 loc) · 2 KB
/
Menu_Driven_Code_for_Stack.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Author: Himanshu Agarwal
Github: himanshu-03 (http://github.com/himanshu-03)
LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
# Menu Driven Code for Stack
"""
class S:
stack = []
MaxSize = 0
tos = 0
def createStack(self, size):
S.tos = -1
S.MaxSize = size
for i in range(0, S.MaxSize):
S.stack.append(0)
print('Stack created of size', len(S.stack))
print(S.stack)
def push(self, e):
S.tos += 1
S.stack[S.tos] = e
print('Element pushed')
def pop(self):
temp = S.stack[S.tos]
S.tos -= 1
return temp
def isFull(self):
if S.tos == S.MaxSize-1:
return True
else:
return False
def isEmpty(self):
if S.tos == -1:
return True
else:
return False
def peek(self):
return (S.stack[S.tos])
def printStack(self):
for i in range(S.tos, -1, -1):
print(i, S.stack[i])
# Main Code
o = S()
o.createStack(int(input('Enter size: ')))
while True:
print('------------')
print('1.Push\n2.Pop\n3.Peek\n4.Print\n0.Exit')
print('------------')
ch = int(input('Enter your choice: '))
if ch == 1:
if o.isFull() != True:
data = int(input('Enter the element to push: '))
o.push(data)
print('Element pushed:', data)
else:
print('\nStack is full..!!')
elif ch == 2:
if o.isEmpty() != True:
print('Element popped:', o.pop())
else:
print('\nStack is empty..!!')
elif ch == 3:
if o.isEmpty() != True:
print('Peeked Element', o.peek())
else:
print('\nStack is empty..!!')
elif ch == 4:
if o.isEmpty() != True:
print('ELements in stack are: ')
o.printStack()
else:
print('\nStack is empty..!!')
elif ch == 0:
break
else:
print('Wrong Input')
1