Q. Each node of a STACK contains the following information :

(i) Pin code of a city,

(ii) Name of city

Write a program to implement following operations in above stack

(a) PUSH () To push a node in to the stack.

(b) POP( ) To remove a rode from the stack.


You can understand by Watching video :-



Answer =
stack = [ ]
while True :
    print()
    print("Enter your choice as per given -")
    print("1 = For insert data Enter insert ")
    print("2 = For delete data enter delete ")
    print("3 = For Exit  enter exit ")
    print()
    user = input("Enter your choice :- ")
    if user == "insert" :
        pin = int(input("Enter the pin code of city :- "))
        city = input("Enter name of city :- ")
        data = [ pin , city ]
        stack.append(data)
    elif user == "delete" :
        if stack == [ ]:
            print("Under Flow")
        else :
            stack.pop()
    else :
        break
    print("Now our stack = ",stack)

Output :-

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- insert
Enter the pin code of city :- 100059
Enter name of city :- Delhi
Now our stack =  [[100059, 'Delhi']]

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- insert
Enter the pin code of city :- 000000
Enter name of city :- India
Now our stack =  [[100059, 'Delhi'], [0, 'India']]

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- insert
Enter the pin code of city :- 168974
Enter name of city :- Lucknow
Now our stack =  [[100059, 'Delhi'], [0, 'India'], [168974, 'Lucknow']]

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- delete
Now our stack =  [[100059, 'Delhi'], [0, 'India']]

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- delete
Now our stack =  [[100059, 'Delhi']]

Enter your choice as per given -
1 = For insert data Enter insert
2 = For delete data enter delete
3 = For Exit  enter exit

Enter your choice :- exit

>>>

10 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. thank you
    just the spelling of choice is wrong , but its fine as it does affect the code

    ReplyDelete
  2. why didnt you create pre defined functions for push and pop ?

    ReplyDelete
    Replies
    1. Because question does not want to make function.

      Delete
  3. check part (a) and (b) stating push() and pop()

    ReplyDelete
  4. this means that we have to define the functions right ?

    ReplyDelete
  5. (b) POP( ) To remove a rode from the stack. In that node will come spelling is wrong

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post