Each node of stack contains the following information : i) pin code of a city ii) Name of city

Write a python program to implement following operations in above stack

a) PUSH() To push a node into a stack

b) POP() To remove a node from stack

Python program to implement push and pop operations

def isEmpty(stk):
    if stk == []:
        return True
    else:
        return False

def push(stk,item):
    stk.append(item)
    top = len(stk)-1
    
def pop(stk):
    if(stk==[]):
        print("Stack empty;UNderflow")
    else:
        print("Deleted City is :",stk.pop())
    
            
stack=[]
top = None
while True:
    print("STACK OPERATION:")
    print("1.ADD City")
    print("2.REMOVE City")
    print("3.Exit")
    ch = int(input("Enter your choice(1-3):"))
    if ch==1:
        pincode = int(input("Enter Pin Code of a city to be inserted :"))
        cname = input("Enter City Name to be inserted :")
        item = [pincode,cname]
        push(stack,item)
        input()
    elif ch==2:
        pop(stack)
        input()    
    elif ch==3:
        break
    else:
        print("Invalid choice ")
        input()

    
             



Output

STACK OPERATION:
1.ADD City
2.REMOVE City
3.Exit
Enter your choice(1-3):1
Enter Pin Code of a city to be inserted :440027
Enter City Name to be inserted :Nagpur

STACK OPERATION:
1.ADD City
2.REMOVE City
3.Exit
Enter your choice(1-3):2
Deleted City is : [440027, 'Nagpur']

STACK OPERATION:
1.ADD City
2.REMOVE City
3.Exit
Enter your choice(1-3):3
<

Leave a Reply

Your email address will not be published.