1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| class Node:
def __init__(self,key):
self.key = key
self.data = None
self.next = None
class HashTable:
def __init__(self,size):
self.size = size
self.array = [None] * self.size
# 哈希函数,根据ASCII表中的值相加,除以表长度
def hash(self,aString):
num = 0
for s in aString:
num += ord(s)
return num % self.size
def get(self,key):
slots = self.hash(key)
head = self.array[slots]
while head:
if head.key == key: #找到相同的键
break
head = head.next
if head == None: # 整条链表中都不存在相同的键,返回None
return None
else:
return head.data
def __setitem__(self, key, value):
slots = self.hash(key)
head = self.array[slots]
if head == None: # 存放的数据是第一个数据,存在头结点
self.array[slots] =Node(key)
self.array[slots].data =value
else:
while head: # 遍历链表
if head.key == key: #发现相同的键,修改值,跳出循环
head.data = value
break
if head.next==None: # 判断节点是否到达尾部
head.next = Node(key)
head.next.data =value
head = head.next
def __getitem__(self, key):
return self.get(key)
h = HashTable(11)
print("创建键值对")
h['fruits'] = 'apple'#创建键值对
h['car'] = 'baoma'
h['student'] = 'xiaoming'
h['sports'] = 'run'
print("创建键值对",h['fruits'],h['car'],h['student'],h['sports'])
h['sports'] = 'walk' # 修改键值对
print("修改键(sports)中的值为:",h['sports'])
h['sport1'] = 'test'
print("冲突的键值对(sports1)",h['sport1'])
print("不存在的键值对",h['fdsfds'])# 不存在的键
|