0% found this document useful (0 votes)
14 views4 pages

Use of Hash Table To Implement Telephone 1

The document contains a Python implementation of a phonebook system using hash tables with two collision handling methods: linear probing and double hashing. It defines classes for records and hash tables, providing methods for inserting, searching, and displaying records. The main function allows users to interact with the system through a command-line interface.

Uploaded by

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

Use of Hash Table To Implement Telephone 1

The document contains a Python implementation of a phonebook system using hash tables with two collision handling methods: linear probing and double hashing. It defines classes for records and hash tables, providing methods for inserting, searching, and displaying records. The main function allows users to interact with the system through a command-line interface.

Uploaded by

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

class Record:

def _init_(self):
self._name = None
self._number = None

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):
record = "Name: " + str(self.get_name()) + "\t\tNumber: " +
str(self.get_number())
return record

def input_record():
record = Record()
name = input("Enter Name: ")
number = int(input("Enter Number: "))
record.set_name(name)
record.set_number(number)
return record

class hashTable:
def _init_(self):
self.size = int(input("Enter the Size of the hash table: "))
self.table = [None for _ in range(self.size)]
self.elementCount = 0
self.comparisons = 0

def isFull(self):
return self.elementCount == self.size

def hashFunction(self, element):


return element % self.size

def insert(self, record):


if self.isFull():
print("Hash Table Full")
return False

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

if self.table[position] is None:
self.table[position] = record
print("Phone number of " + record.get_name() + " is at position " +
str(position))
self.elementCount += 1
else:
print("Collision has occurred for " + record.get_name() + "'s phone
number at position "
+ str(position) + ". Finding new position.")
while self.table[position] is not None:
position = (position + 1) % self.size
self.table[position] = record
print("Phone number of " + record.get_name() + " is at position " +
str(position))
self.elementCount += 1
return True

def search(self, record):


position = self.hashFunction(record.get_number())
self.comparisons = 1
start_position = position

while self.table[position] is not None and self.comparisons <= self.size:


if (self.table[position].get_name() == record.get_name() and
self.table[position].get_number() == record.get_number()):
print("Phone number found at position {} and total comparisons are
{}".format(position, self.comparisons))
return position
position = (position + 1) % self.size
self.comparisons += 1
if position == start_position:
break

print("Record not found")


return False

def display(self):
print("\n")
for i in range(self.size):
print("Hash Value: " + str(i) + "\t\t" + str(self.table[i]))
print("The number of phonebook records in the Table are: " +
str(self.elementCount))

class doubleHashTable:
def _init_(self):
self.size = int(input("Enter the Size of the hash table: "))
self.table = [None for _ in range(self.size)]
self.elementCount = 0
self.comparisons = 0

def isFull(self):
return self.elementCount == self.size

def h1(self, element):


return element % self.size

def h2(self, element):


return 5 - (element % 5)

def doubleHashing(self, record):


limit = self.size
i = 1
while i <= limit:
newPosition = (self.h1(record.get_number()) + i *
self.h2(record.get_number())) % self.size
if self.table[newPosition] is None:
return True, newPosition
i += 1
return False, None

def insert(self, record):


if self.isFull():
print("Hash Table Full")
return False

position = self.h1(record.get_number())
if self.table[position] is None:
self.table[position] = record
print("Phone number of " + record.get_name() + " is at position " +
str(position))
self.elementCount += 1
else:
print("Collision has occurred for " + record.get_name() + "'s phone
number at position "
+ str(position) + ". Finding new position.")
posFound, position = self.doubleHashing(record)
if posFound:
self.table[position] = record
self.elementCount += 1
print("Phone number of " + record.get_name() + " is at position " +
str(position))
else:
print("Could not find position")
return True

def search(self, record):


position = self.h1(record.get_number())
self.comparisons = 1

if self.table[position] is not None and self.table[position].get_name() ==


record.get_name():
print("Phone number found at position {} and total comparisons are
{}".format(position, self.comparisons))
return position

limit = self.size
i = 1
while i <= limit:
position = (self.h1(record.get_number()) + i *
self.h2(record.get_number())) % self.size
self.comparisons += 1
if self.table[position] is not None and self.table[position].get_name()
== record.get_name():
print("Phone number found at position {} and total comparisons are
{}".format(position, self.comparisons))
return position
i += 1

print("Record not found")


return False

def display(self):
print("\n")
for i in range(self.size):
print("Hash Value: " + str(i) + "\t\t" + str(self.table[i]))
print("The number of phonebook records in the Table are: " +
str(self.elementCount))

def main():
choice1 = 0
while choice1 != 3:
print("\nCollision Handling Methods")
print("1. Linear Probing")
print("2. Double Hashing")
print("3. Exit")
choice1 = int(input("Enter Your Choice: "))
if choice1 > 3:
print("Please Enter Valid Choice")
elif choice1 == 1:
ha1 = hashTable()
choice2 = 0
while choice2 != 4:
print("\n1. Insert")
print("2. Search")
print("3. Display")
print("4. Back")
choice2 = int(input("Enter Your Choice: "))
if choice2 > 4:
print("Please Enter Valid Choice")
elif choice2 == 1:
record = input_record()
ha1.insert(record)
elif choice2 == 2:
record = input_record()
ha1.search(record)
elif choice2 == 3:
ha1.display()
elif choice1 == 2:
ha2 = doubleHashTable()
choice2 = 0
while choice2 != 4:
print("\n1. Insert")
print("2. Search")
print("3. Display")
print("4. Back")
choice2 = int(input("Enter Your Choice: "))
if choice2 > 4:
print("Please Enter Valid Choice")
elif choice2 == 1:
record = input_record()
ha2.insert(record)
elif choice2 == 2:
record = input_record()
ha2.search(record)
elif choice2 == 3:
ha2.display()

if _name_ == "_main_":
main()

You might also like