-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashSet.py
51 lines (51 loc) · 1.51 KB
/
HashSet.py
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
class HashNode:
def __init__(self,d=0,n=None):
self.data=d
self.next=n
class HashSet:
def __init__(self):
self.ele=[None]*10
self.capacity=10
self.mysize=0
def HashCode(self,val):
return abs(val)%self.capacity
def add(self,val):
if self.contains(val)==False:
bucket=self.HashCode(val)
self.ele[bucket]=HashNode(val,self.ele[bucket])
self.mysize+=1
def contains(self,val):
bucket=self.HashCode(val)
cur=self.ele[bucket]
while cur!=None:
if cur.data==val:
return True
cur=cur.next
return False
def remove(self,val):
if self.contains(val)==False:
return
bucket=self.HashCode(val)
if self.ele[bucket].data==val:
self.ele[bucket]=self.ele[bucket].next
else:
cur=self.ele[bucket]
while cur.next!=None:
if cur.next.data==val:
cur.next=cur.next.next
self.mysize-=1
return
if cur.next==None:
break
else:
cur=cur.next
def toString(self):
for i in range(0,len(self.ele)):
if self.ele[i]==None:
print('//')
else:
cur=self.ele[i]
while cur!=None:
print(cur.data,end='->')
cur=cur.next
print()