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


You can understand by Watching video :-



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)
    print("Your Stack :-",stack)

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

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

Output :-


Enter ( Push or  Pop ) : push
Enter a number : 6
Your Stack :- [6]
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : push
Enter a number : 3
Your Stack :- [3, 6]
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : push
Enter a number : 9
Your Stack :- [9, 3, 6]
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : pop
Your Stack :- [3, 6]
Enter ( Yes or no ) : no
Thank you !!!!

>>>

8 Comments

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

Post a Comment

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

Previous Post Next Post