0% found this document useful (0 votes)
2 views

dsa1

The document defines a Hashtable class that implements two methods for handling collisions: separate chaining and linear probing. It provides functionality to insert and search for telephone numbers using both methods, allowing users to add and retrieve numbers interactively. The program continues to run until the user chooses to exit.

Uploaded by

cha9rul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

dsa1

The document defines a Hashtable class that implements two methods for handling collisions: separate chaining and linear probing. It provides functionality to insert and search for telephone numbers using both methods, allowing users to add and retrieve numbers interactively. The program continues to run until the user chooses to exit.

Uploaded by

cha9rul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

class Hashtable:

def __init__(self,size):

self.size=size

self.table=[None]*size

def hash_function(self, key):

return hash(key)%self.size

def insert_separate_chaining(self,key,value):

index=self.hash_function(key)

if self.table[index] is None:

self.table[index]=[(key,value)]

else:

self.table[index].append((key,value))

def search_separate_chaining(self,key):

index=self.hash_function(key)

if self.table[index] is not None:

for item in self.table[index]:

if item[0]==key:

return item[1]

return None

def insert_linear_probing(self,key,value):

index=self.hash_function(key)

while self.table[index] is not None:

index=(index+1)%self.size

self.table[index]=(key,value)

def search_linear_probing(self,key):

index=self.hash_function(key)
og_index=index

while self.table[index] is not None:

if self.table[index][0] ==key:

return self.table[index][1]

index=(index+1)%self.size

if index==og_index:

break

return None

hash_table_chaining=Hashtable(10)

hash_table_linear_probing=Hashtable(10)

while True:

print("\n1. Add a telephone number")

print("2. Search for a telephone number")

print("3. Exit\n")

choice=input("Enter your choice (1/2/3): ")

if choice == "1":

name=input("Enter the name: ")

number=input("Enter the telephone number: ")

hash_table_chaining.insert_separate_chaining(name, number)

hash_table_linear_probing.insert_linear_probing(name, number)

print("Telephone number added successfully!")

elif choice == "2":

name=input("Enter name to search: ")


result_chaining=hash_table_chaining.search_separate_chaining(name)

result_linear_probing=hash_table_linear_probing.search_linear_probing(name)

if result_chaining:

print(f"Separate Chaining - {name}'s number: {result_chaining}")

else:

print(f"Separate Chaining - {name} not found.")

if result_linear_probing:

print(f"Linear Probing - {name}'s number: {result_linear_probing}")

else:

print(f"Linear Probing - {name} not found.")

elif choice == "3":

print("Exiting Program...")

break

else:

print("Invalid choice!! Please enter 1,2 or 3.")

OUTPUT:

You might also like