Stack in DSA Python

Program 1

# Stack Implementation
stack=list()
def push():
    n=int(input("Enter element in stack:"))
    if(len(stack)==0):
        stack.append(n)
    else:
        stack.insert(0,n)   

def pop():
     if(len(stack)==0):
        print("Stack is empty")
     else:
         print("Poped element is:",stack[0])   
         del stack[0] 
def display():
    if(len(stack)==0):
        print("Stack is empty")
    else:
        print("Stack elements: ")    
        for element in stack:
            print(element)


while(1):
    print("-------------------Stack Menu-------------------")
    print("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit")
    print("---------------------------------------------------")
    choice=int(input("Enter your choice:"))
    if(choice==1):
        push()
    elif(choice==2):    
        pop()
    elif(choice==3):        
        display()
    elif(choice==4):            
        print("Ok Bye Bye")
        break
    else:
        print("Invalid choice")

 

 

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 *