Site icon DataFlair

Stack using List in DSA Python

Program 1

# Stack Implementation using list

stack=[] # Empty stack
# push element in stack
def push():
    n=int(input("Enter and element: "))
    if(len(stack)==0):
        stack.append(n)
    else:
        stack.insert(0,n)
# pop element in stack        
def pop():
    if(len(stack)==0):
        print("Stack is empty") 
    else:
        print("Poped element is: ",stack[0]) 
        del stack[0]
# display elements of stack
def display():
      if(len(stack)==0):
        print("Stack is empty") 
      else:
          print("Elements of stack: ")  
          for element in stack:
              print(element)

# Main 
while(1):
    print("------------------Stack Menu--------------------")
    print("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()
    else:
        break

 

Exit mobile version