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

exp1dsa

The document contains a Python implementation of a hash table using linear probing and double hashing for collision resolution. It includes classes for managing records and the hash table, along with methods for inserting, searching, and displaying records. A driver code allows users to interactively choose a hashing method and perform operations on the hash table.

Uploaded by

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

exp1dsa

The document contains a Python implementation of a hash table using linear probing and double hashing for collision resolution. It includes classes for managing records and the hash table, along with methods for inserting, searching, and displaying records. A driver code allows users to interactively choose a hashing method and perform operations on the hash table.

Uploaded by

Vaibhav Hoke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name : Vaibhav hoke

Roll no. : COSA69

Assignment 1 :

Input :

class Record:

def __init__(self, name=None, number=None):

self.name = name

self.number = number

def get_name(self):

return self.name

def get_number(self):

return self.number

def set_name(self, name):

self.name = name

def set_number(self, number):

self.number = number

def __str__(self):

return f"Name: {self.name}, Number: {self.number}"

class HashTable:

def __init__(self, size):

self.size = size

self.table = [None] * size

self.elementCount = 0
def isFull(self):

return self.elementCount == self.size

def hashFunction(self, number):

return number % self.size

def insert(self, record):

if self.isFull():

print("Hash Table is full!")

return False

position = self.hashFunction(record.get_number())

initial_position = position

while self.table[position] is not None:

print(f"Collision at {position}, trying next position...")

position = (position + 1) % self.size

if position == initial_position: # Full circle

print("Unable to insert. No free slot.")

return False

self.table[position] = record

self.elementCount += 1

print(f"{record} inserted at position {position}")

return True

def search(self, number):

position = self.hashFunction(number)

initial_position = position

while self.table[position] is not None:


if self.table[position].get_number() == number:

print(f"Record found at position {position}: {self.table[position]}")

return position

position = (position + 1) % self.size

if position == initial_position: # Full circle

break

print("Record not found!")

return -1

def display(self):

print("\nHash Table Contents:")

for i, record in enumerate(self.table):

print(f"Position {i}: {record if record else 'Empty'}")

class DoubleHashTable(HashTable):

def __init__(self, size):

super().__init__(size)

def h2(self, number):

return 5 - (number % 5)

def insert(self, record):

if self.isFull():

print("Hash Table is full!")

return False

position = self.hashFunction(record.get_number())

step = self.h2(record.get_number())

initial_position = position
while self.table[position] is not None:

print(f"Collision at {position}, trying next position using step size {step}...")

position = (position + step) % self.size

if position == initial_position: # Full circle

print("Unable to insert. No free slot.")

return False

self.table[position] = record

self.elementCount += 1

print(f"{record} inserted at position {position}")

return True

# Driver Code

def input_record():

name = input("Enter Name: ")

number = int(input("Enter Phone Number: "))

return Record(name, number)

def main():

print("Choose Hashing Method:")

print("1. Linear Probing")

print("2. Double Hashing")

choice = int(input("Enter your choice: "))

size = int(input("Enter size of the hash table: "))

if choice == 1:

hash_table = HashTable(size)

elif choice == 2:
hash_table = DoubleHashTable(size)

else:

print("Invalid choice!")

return

while True:

print("\nMenu:")

print("1. Insert")

print("2. Search")

print("3. Display")

print("4. Exit")

option = int(input("Enter your choice: "))

if option == 1:

record = input_record()

hash_table.insert(record)

elif option == 2:

number = int(input("Enter Phone Number to Search: "))

hash_table.search(number)

elif option == 3:

hash_table.display()

elif option == 4:

print("Exiting...")

break

else:

print("Invalid option!")

if __name__ == "__main__":

main()
Output :

#[?2004l

Choose Hashing Method:

1. Linear Probing

2. Double Hashing

Enter your choice: 1

Enter size of the hash table: 3

Menu:

1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 1

Enter Name: mir

Enter Phone Number: 1234556

Name: mir, Number: 1234556 inserted at position 2

Menu:

1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 1

Enter Name: gig

Enter Phone Number: 123467

Collision at 2, trying next position...

Name: gig, Number: 123467 inserted at position 0


Menu:

1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 1

Enter Name: ersre

Enter Phone Number: 123547

Name: ersre, Number: 123547 inserted at position 1

Menu:

1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 2# #1

Enter Name: rre

Enter Phone Number: 5678

Hash Table is full!

Menu:

1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 2

Enter Phone Number to Search: 1234556

Record found at position 2: Name: mir, Number: 1234556

Menu:
1. Insert

2. Search

3. Display

4. Exit

Enter your choice: 1# #2

Enter Phone Number to Search: 23344

Record not found!

You might also like