STACK - Dhruv

stack=[]

def push():
    while True:
        n=input("Enter element:")
        stack.append(n)
        print("Element added Successfully !!")
        ch=input("wanna add more?(y/n):")
        if ch.upper()=='N':
            break

def pop():
    if stack==[]:
        print("Your Stack is empty !!")
    else:
        x=stack.pop()
        print ("Element removed successfully !!")
        print ("Removed element:",x)

def show():
    if stack==[]:
        print("Your Stack is empty !!")
    else:
        l=len(stack)
        print("Your stack elements are:")
        for i in range(l-1,-1,-1):
            print(stack[i])

def clear():
    stack.clear()
    print("Your stack is cleared !!")

def main():
    print('''
Stack Functions:
1.Add element
2.Delete element
3.Show stack
4.Clear stack''')


main()
ch=int(input("Enter your choice:"))
if ch==1:
    push()
elif ch==2:
    pop()
elif ch==3:
    show()
elif ch==4:
    clear()
else:
    print("Sorry !! Wrong Choice.")
    

Comments