Stack using Collection Module in DSA Python

Program 1

import collections
stack=collections.deque()
while(1):
    print("\n----------------------------Stack Menu-------------------\n")
    print("1.Push\n2.Pop\n3.Display\n4.Exit ")
    print("\n------------------------------------------------------------\n")
    choice=int(input("Enter your choice"))
    if(choice==1):
        n=int(input("Enter an element for push"))
        stack.append(n)
    elif(choice==2):
        if(not stack):
            print("Stack is empty")    
        else:
            print("Poped element:",stack.pop())
    elif(choice==3):
        if(not stack):
            print("Stack is empty")    
        else:
            for element in stack:
                print(element, end=" ")
    else:
        break        


  

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *