Q. A linear Stack called Directory contains the following information:
-- Pin code of the city
-- Name of city
Write add(Directory) and delete(Directory) methods in Python to add and remove contacts using append() and pop() operations in Stack.
Answer =
def add(Directory) : stack.append(Directory) def delete() : stack.pop() stack = [ ] while True : print() print("Enter your choise 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 choise :- ") if user == "insert" : pin = float(input("Enter the pin :- ")) name = input("Enter the name :- ") Directory = [ pin , name ] add(Directory) elif user == "delete" : if stack == [ ]: print("UnderFlow") else : delete() 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 :- 231209
Enter the name :- Kanpur
Now our stack = [[231209.0, 'Kanpur']]
1. For insert data Enter 'insert'
2. For delete data enter 'delete'
3. For Exit enter 'exit'
Enter your choice:- insert
Enter the pin :- 231209
Enter the name :- Kanpur
Now our stack = [[231209.0, 'Kanpur']]
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 :- 548967
Enter the name :- goa
Now our stack = [[231209.0, 'Kanpur'], [548967.0, 'goa']]
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 = [[231209.0, 'Kanpur']]
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 :- 581436
Enter the name :- delhi
Now our stack = [[231209.0, 'Kanpur'], [581436.0, '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
>>>
cant we use push instead of append ?
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )