DSA Practical Dictionary ADT
DSA Practical Dictionary ADT
Problem Statement:
Implement all the functions of a dictionary (ADT) using hashing and handle collisions using
chaining with/without replacement. Data: Set of (key, value) pairs, Keys are mapped to
values, Keys must be comparable, Keys must be unique. Standard Operations: Insert(key,
value), Find(key), Delete(key).
Python Implementation:
class Dictionary:
def __init__(self, size=10, with_replacement=False):
self.size = size
self.table = [[] for _ in range(size)]
self.with_replacement = with_replacement
if self.with_replacement:
for idx, (existing_key, _) in enumerate(bucket):
if existing_key == key:
bucket[idx] = (key, value)
print(f"Key '{key}' updated with new value '{value}' (with replacement).")
return
else:
for (existing_key, _) in bucket:
if existing_key == key:
print(f"Key '{key}' already exists (no replacement).")
return
bucket.append((key, value))
print(f"Key '{key}' inserted with value '{value}'.")
for k, v in bucket:
if k == key:
return v
return None
def display(self):
print("\nCurrent Hash Table:")
for index, bucket in enumerate(self.table):
if bucket:
print(f"Index {index}: {bucket}")
else:
print(f"Index {index}: Empty")
def menu_driven():
replacement_choice = input("Do you want to use chaining with replacement? (y/n):
").lower()
with_replacement = True if replacement_choice == 'y' else False
while True:
print("\nMenu:")
print("1. Insert (key, value)")
print("2. Find (key)")
print("3. Delete (key)")
print("4. Display Dictionary")
print("5. Exit")
choice = input("\nEnter your choice (1/2/3/4/5): ")
if choice == '1':
try:
key = int(input("Enter key (integer): "))
value = input("Enter value (string): ")
dict_obj.insert(key, value)
except ValueError:
print("Invalid input. Key must be an integer.")
else:
print("Invalid choice. Please try again.")
menu_driven()