Problem Statement:
Consider telephone book database of N clients. Make use of a hash table implementation to quickly look up client‘s telephone Number. Make Use of two Collision Techniques and Compare them using number of Comparisons required to find a set of telephone numbers.
#Coding for Linear Probing Without Replacement
print("•••••Program for Hash Table•••••")
m=int(input("Enter table size:"))
#Array for Hash Table
ht=[]
#Array to store keys
a=[]
#Initializing hashtable with -1
for i in range(0, m):
ht.append(-1)
#Printing hash table before adding elements
print("Hash Table before adding elements")
for i in range(0, m):
print(i,"---",ht[i])
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
#Code for mainmenu
def main():
print("Main menu")
print("1.Enter Keys")
print("2.Display Hash Table")
print("3.Exit")
print("Enter your choice:")
ch=int(input())
if(ch==1):
insert_keys()
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
main()
elif(ch==2):
display()
print("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
main()
elif(ch==3):
exit
else:
print("Enter valid choice...")
print ("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°")
main()
#Code for accepting keys from user
#Code to perform hashing on it
def insert_keys():
global n
flag=0
print("//////////////////////////////////")
n=int(input("Enter Total number of keys:"))
for i in range(0, n):
global key
key=int(input("Enter key:"))
c=key%m
if(ht[c]==-1):
ht[c]=key
else:
print("Collision occurs at index",c)
print("Finding next empty location for",key)
for i in range(c, m):
if(ht[i]==-1):
ht[i]=key
flag=1
break
if(flag==0):
for i in range(0, c):
if(ht[i]==-1):
ht[i]=key
flag=1
break
a.append(key)
print("The keys you enter are:-")
print(a)
print("Performed hasing on the above data!!!")
#Code to display Hash Table
def display():
print("*Hash Table**")
print("Index and Value")
for i in range(m):
print(i,"---",ht[i])
#Calling main function
main()
# OUTPUT
Comments
Post a Comment