DSA Practical Telephone Book
DSA Practical Telephone Book
Problem Statement:
Consider a telephone book database of N clients. Make use of a hash table implementation
to quickly look up a client's telephone number. Use two collision handling techniques and
compare them using the number of comparisons required to find a set of telephone
numbers.
Python Code:
class TelephoneBook:
def __init__(self, name, tel_no):
self.name = name
self.tel_no = tel_no
def Insertion_LinearProbing():
hashtable = [None for i in range(10)]
num_records = int(input("\nEnter number of records: "))
for i in range(num_records):
n = input("Enter name: ")
t = int(input("Enter telephone no.: "))
hashValue = t % 10
j=0
while hashtable[hashValue] is not None:
j += 1
hashValue = (hashValue + 1) % 10
hashtable[hashValue] = TelephoneBook(n, t)
return hashtable
def Insertion_QuadProbing():
hashtable = [None for i in range(10)]
num_records = int(input("\nEnter number of records: "))
for i in range(num_records):
n = input("Enter name: ")
t = int(input("Enter telephone no.: "))
hashValue = t % 10
j=0
while hashtable[hashValue] is not None:
j += 1
hashValue = (t % 10 + j * j) % 10
hashtable[hashValue] = TelephoneBook(n, t)
return hashtable
def Display(hash_table):
print("-------------------------------")
print("Index\tName\tTelephone No.")
print("-------------------------------")
for i, obj in enumerate(hash_table):
if obj is None:
print(f"{i}\t-\t-")
else:
print(f"{i}\t{obj.name}\t{obj.tel_no}")
print("-------------------------------")
def main():
hash_linear = [None for i in range(10)]
hash_quad = [None for i in range(10)]
print("-------------------------------")
print(" Telephone Book Database")
print("\nInserting data using Linear Probing...")
hash_linear = Insertion_LinearProbing()