REVERSE USING STACK
REVERSE USING STACK IN PYTHON
class stack:
def __init__(self):
self.top=0
self.stack2=[]
self.stack=[]
self.maxx=int(input("Enter the array size: "))
self.top2=self.maxx-1
def pushh(self,item):
self.top+=1
self.stack.append(item)
def popp(self):
if self.top!=0:
self.top-=1
print self.stack.pop()
else:
print "stack underflow"
def display(self):
print "STACK IS: \n"
if self.top!=0:
print self.stack
else:
print "StacK underflow"
print "REVERSE STACK IS: \n"
if self.top2!=(self.maxx-1):
print self.stack2
else:
print("StacK underflow")
def reverse(self):
self.stack2=[]
if self.top2!=0:
for i in range(len(self.stack)-1,-1,-1):
self.stack2.append(self.stack[i])
self.top2-=1
else:
print("Stack underflow")
s1=stack()
while(True):
print("\n\n1. display")
print("2. Pop top element" )
print("3. push an element")
print("4. reverse the stack")
print("5. exit")
ch=int(input("Enter the choice: "))
if ch==1:
s1.display()
elif ch==2:
s1.popp()
elif ch==3:
if s1.top<s1.maxx:
m=int(input("Enter value: "))
s1.pushh(m)
else:
print("stack overflow")
elif ch==4:
s1.reverse()
else:
exit()
Comments
Post a Comment