Q. Write a program that, depending upon the user's choice, either pushes or pops an element in a Stack. The elements are shifted towards the right so that the top always remains at 0th (zeroth) index.

Answer =

stack = [ ]
while True :
    com = input("Enter ( Push or  Pop ) : ")
    
    if com == "push" or com == "Push" :
        num = int(input("Enter a number : "))
        stack.insert(0,num)

    elif  len(stack) == 0 :
        print("Underflow")
        
    elif com == "pop" or com == "Pop" :
        stack.pop(0)

    yes = input ("Enter ( Yes or no ) : ")

    if yes == "No" or yes == "no":
        print("Thank you !!!!")
        break

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post