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

DSA PRG1

The document outlines a Python program that implements a simple telephone directory using a hash table. It includes functionalities for inserting, displaying, searching, and deleting phone numbers associated with names. The hash table is designed with 10 slots and utilizes a basic hash function for indexing.

Uploaded by

AYAAN TAMBOLI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA PRG1

The document outlines a Python program that implements a simple telephone directory using a hash table. It includes functionalities for inserting, displaying, searching, and deleting phone numbers associated with names. The hash table is designed with 10 slots and utilizes a basic hash function for indexing.

Uploaded by

AYAAN TAMBOLI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No : 1 16

PROGRAM: 14

class Node:
def __init__(self, name, value):
self.name = name
self.value = value
self.next = None

class HashTable:
def __init__(self):
self.hash_table = [None] * 10 # Create hash table with 10 slots

def hash_function(self, value):


return value % 10

def display(self):
for i in range(10):
temp = self.hash_table[i]
print(f"a[{i}] : ", end="")
while temp:
print(f"->{temp.name} ({temp.value})", end=" ")
temp = temp.next
print()

def search_element(self, value):


hash_val = self.hash_function(value)
entry = self.hash_table[hash_val]
while entry:
if entry.value == value:
return f"Element found at index {hash_val}: {entry.name} ({entry.value})"
entry = entry.next
return "No element found"

def delete_element(self, value):


hash_val = self.hash_function(value)
entry = self.hash_table[hash_val]
if not entry:
print("No element found.")
return
if entry.value == value:
self.hash_table[hash_val] = entry.next
return
while entry.next and entry.next.value != value:
entry = entry.next
if not entry.next:
print("No element found.")
return
entry.next = entry.next.next

def insert_element(self, name, value):


hash_val = self.hash_function(value)
node = Node(name, value)
temp = self.hash_table[hash_val]
if not temp:
self.hash_table[hash_val] = node
else:
while temp.next:
temp = temp.next
temp.next = node

h = HashTable()

while True:
print("\nTelephone Directory: \n1.Insert \n2.Display \n3.Search \n4.Delete \n5.Exit")
try:
choice = int(input("\nOPTION: "))
except ValueError:
print("\nInvalid input. Please enter a valid option.")
continue

if choice == 1:
name = input("\nEnter name: ")
data = int(input("Enter phone number: "))
h.insert_element(name, data)
elif choice == 2:
h.display()
elif choice == 3:
search = int(input("\nEnter the phone number to be searched: "))
result = h.search_element(search)
print(result)
elif choice == 4:
del_val = int(input("\nEnter the phone number to be deleted: "))
h.delete_element(del_val)
print("Phone number deleted")
elif choice == 5:
break
else:
print("\nInvalid choice, please try again.")
OUTPUT:

You might also like