Â
In this we are going to see a program on stack using array in Python Programming Language.
import array as arr
class Stack:
def __init__(self,size):
self.stk=arr.array('i',[])
self.top=-1
self.size=size
def spush(self):
if self.top==self.size:
print("Stack is full")
else:
val=int(input("Enter value to be pushed: "))
self.stk.append(val)
self.top+=1
def spop(self):
if self.top<0:
print("Stack is empty")
else:
self.stk.pop()
self.top-=1
def display(self):
for i in self.stk:
print(i,end=" ")
print("")
s=Stack(4)
while(True):
print("Choice 1: Push onto stack.")
print("Choice 2: Pop Stack.")
print("Choice 3: Display Stack elements.")
print("Choice 4: Exit.")
ch=int(input("Enter choice: "))
if ch==1:
s.spush()
elif ch==2:
s.spop()
elif ch==3:
s.display()
elif ch==4:
break
# Coded by Saahil
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP