CBSE Class 12 Computer Science
Question 38 of 63
Data Structures in Python — Question 12
Back to all questions 12
Question 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.
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)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']]