python 散列表查找
2021-06-20 05:06
标签:散列 ret == htm res lse return index ini python 散列表查找 标签:散列 ret == htm res lse return index ini 原文地址:https://www.cnblogs.com/sea-stream/p/9689125.htmlclass HashTable:
def __init__(self, size):
self.elem = [None for i in range(size)]
self.count = size #
def hash(self, key):
return key % self.count #
def insert_hash(self, key):
address = self.hash(key) #
while self.elem[address]: #
address = (address+1) % self.count #
self.elem[address] = key #
def search_hash(self, key):
star = address = self.hash(key)
while self.elem[address] != key:
address = (address + 1) % self.count
if not self.elem[address] or address == star: #
return False
return True
list_a = [12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34]
hash_table = HashTable(12)
for i in list_a:
hash_table.insert_hash(i)
for i in hash_table.elem:
if i:
print((i, hash_table.elem.index(i)), end=" ")
print("n")
print(hash_table.search_hash(15))
print(hash_table.search_hash(33))
上一篇:python 冒泡排序
下一篇:C语言程序的机器级表示