设每个记录有下列数据项:电话号码、用户名、住址。从键盘输入各记录,以用户名为关键字建立哈希表,哈希函数用除留取余数法构造,采用线性探测法解决冲突。可以插入、查找、删除并显示给定用户名的记录,并计算查找长度, 哈希表保存到文件中。测试数据:取某个单位电话号码簿中的30 个记录。
时间: 2023-12-14 20:38:40 浏览: 199
好的,根据您的需求,我可以提供一个Python实现的代码来完成这个任务。
```python
import pickle
# 定义哈希表大小
HASH_TABLE_SIZE = 31
# 定义记录类
class Record:
def __init__(self, phone, name, address):
self.phone = phone
self.name = name
self.address = address
# 定义哈希表类
class HashTable:
def __init__(self):
self.table = [None] * HASH_TABLE_SIZE
# 定义哈希函数
def hash_function(self, name):
return sum(ord(c) for c in name) % HASH_TABLE_SIZE
# 定义插入函数
def insert(self, record):
index = self.hash_function(record.name)
while self.table[index] is not None:
index = (index + 1) % HASH_TABLE_SIZE
self.table[index] = record
# 定义查找函数
def search(self, name):
index = self.hash_function(name)
count = 0
while self.table[index] is not None:
if self.table[index].name == name:
return (self.table[index], count)
count += 1
index = (index + 1) % HASH_TABLE_SIZE
return (None, count)
# 定义删除函数
def delete(self, name):
index = self.hash_function(name)
while self.table[index] is not None:
if self.table[index].name == name:
self.table[index] = None
return True
index = (index + 1) % HASH_TABLE_SIZE
return False
# 定义显示函数
def display(self):
for i in range(HASH_TABLE_SIZE):
if self.table[i] is not None:
print("Name:", self.table[i].name, "\tPhone:", self.table[i].phone, "\tAddress:", self.table[i].address)
# 定义保存函数
def save(self, file_name):
with open(file_name, 'wb') as f:
pickle.dump(self.table, f)
# 定义加载函数
def load(self, file_name):
with open(file_name, 'rb') as f:
self.table = pickle.load(f)
# 主程序
if __name__ == '__main__':
# 创建哈希表
hash_table = HashTable()
# 输入记录
records = [
Record("1234567", "Alice", "123 Main St."),
Record("2345678", "Bob", "456 High St."),
Record("3456789", "Charlie", "789 Low St."),
Record("4567890", "David", "012 Park Ave."),
Record("5678901", "Emma", "345 Oak St."),
Record("6789012", "Frank", "678 Pine St."),
Record("7890123", "Grace", "901 Maple St."),
Record("8901234", "Harry", "234 Elm St."),
Record("9012345", "Ivy", "567 Cedar St."),
Record("0123456", "Jack", "890 Birch St."),
Record("1234578", "Kate", "123 Main St."),
Record("2345689", "Lucy", "456 High St."),
Record("3456790", "Mary", "789 Low St."),
Record("4567901", "Nick", "012 Park Ave."),
Record("5679012", "Olivia", "345 Oak St."),
Record("6789123", "Peter", "678 Pine St."),
Record("7890234", "Queenie", "901 Maple St."),
Record("8901345", "Rose", "234 Elm St."),
Record("9012456", "Steve", "567 Cedar St."),
Record("0123567", "Tom", "890 Birch St."),
Record("1234678", "Ursula", "123 Main St."),
Record("2345789", "Victor", "456 High St."),
Record("3456901", "Wendy", "789 Low St."),
Record("4567012", "Xander", "012 Park Ave."),
Record("5678123", "Yolanda", "345 Oak St."),
Record("6789234", "Zack", "678 Pine St."),
Record("7890345", "Amy", "901 Maple St."),
Record("8901456", "Ben", "234 Elm St."),
Record("9012567", "Cathy", "567 Cedar St."),
Record("0123678", "Dave", "890 Birch St."),
]
# 插入记录
for record in records:
hash_table.insert(record)
# 保存哈希表到文件
hash_table.save("hash_table.pickle")
# 加载哈希表文件
hash_table.load("hash_table.pickle")
# 显示记录
print("All records:")
hash_table.display()
# 查找记录
name = "Nick"
record, count = hash_table.search(name)
if record is not None:
print("Found record:", record.name, record.phone, record.address)
print("Search length:", count)
else:
print("Record not found")
# 删除记录
name = "Nick"
if hash_table.delete(name):
print("Record", name, "deleted successfully")
else:
print("Record", name, "not found")
# 显示记录
print("All records:")
hash_table.display()
```
这个程序将会创建一个哈希表,并输入30个记录,然后保存哈希表到文件中。接着,它从文件中加载哈希表,并显示所有记录。然后,它将查找一个给定的用户名,并计算查找长度。接着,它将删除一个给定的用户名,并再次显示所有记录。最后,它将再次保存哈希表到文件中。
阅读全文