PUSH POP DISPLAY STACK IN PYTHON

PUSH POP DISPLAY STACK  IN PYTHON

 

class stack:
    def __init__(self):
        self.top=0
        self.stack=[]
        self.maxx=int(input("Enter the array size: "))
        
    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):
        if self.top!=0:
            print self.stack
        else:
            print("StacK underflow")
    
s1=stack()
while(True):
    print("\n\n1. display")
    print("2. Pop top element" )
    print("3. push an element")
    print("4. 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")
    else:
        exit()

Comments

Popular posts from this blog

SAME NAME IN CLASS METHOD AND CONSTRUCTOR

PALINDROME NUMBER

FIBONACCI SERIES