Computer Science
A linear Stack called Directory contains the following information as contacts:
— Pin code of 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.
Python
Python Stack
4 Likes
Answer
def add(Directory):
pincode = int(input("Enter pincode of city: "))
cityname = input("Enter the city name: ")
contact = [pincode, cityname]
Directory.append(contact)
def delete(Directory):
if (Directory == []):
print("Directory is empty")
else:
print("Deleted contact:", Directory.pop())
Directory = []
add(Directory)
add(Directory)
print("Initial Directory Stack:", Directory)
delete(Directory)
print("Directory Stack after deletion:", Directory)
Output
Enter pincode of city: 586789
Enter the city name: Bangalore
Enter pincode of city: 567890
Enter the city name: Mysore
Initial Directory Stack: [[586789, 'Bangalore'], [567890, 'Mysore']]
Deleted contact: [567890, 'Mysore']
Directory Stack after deletion: [[586789, 'Bangalore']]
Answered By
2 Likes
Related Questions
Write a menu driven python program using queue, to implement movement of shuttlecock in it's box.
Give the necessary declaration of a list implemented Stack containing float type numbers. Also, write a user-defined function to pop a number from this Stack.
Write add(Books) and delete(Books) methods in Python to add Books and Remove Books considering them to act as append() and pop() operations in Stack.
Write AddClient(Client) and DeleteClient(Client) methods in Python to add a new client and delete a client from a list client name, considering them to act as insert and delete operations of the Queue data structure.