class Node:
def __init__(self, name, phone, address, nexxt):
self._name = name
self._phone = phone
self._address = address
self._next = nexxt
def __str__(self):
return "("+self._name+","+self._phone+","+self._address+")"
#from Node import *
MAXSIZE = 100 # number of buckets
class ContactsHT:
@classmethod
def myhash(cls,name):
sum = 0
for x in name:
sum = sum + ord(x)
return sum%MAXSIZE
# hash table for contacts
def __init__(self):
self._hashtable = [Node("","","",None)]*MAXSIZE
#self._hashtable = [Node("","","",None) for i in range(MAXSIZE)]
self._size = 0
def find(self,name):
bnum = ContactsHT.myhash(name)
p = self._hashtable[bnum]
found = False
while p._next != None and not found:
if p._next._name == name:
found = True
else:
p = p._next
return (found,p)
def insert(self,contact):
(found,p) = self.find(contact[0])
if found:
return False
else:
bnum = ContactsHT.myhash(contact[0])
p = Node(contact[0],contact[1],contact[2],self._hashtable[bnum]._next)
self._hashtable[bnum]._next = p
self._size = self._size + 1
return True
def delete(self,name):
#??? homework
def update(self,contact):
#??? homework
def size(self):
return self._size
def __str__(self):
result = "\n"
for hindex in range(MAXSIZE):
p = self._hashtable[hindex]
while p._next != None:
result = result + str(p._next)+"\n"
p = p._next
return result+"\n"
[str(Node("","","",None)) for i in range(3)]
[Node("","","",None)]*3
[9]*12
[9 for i in range(12)]
def myhash(name):
sum = 0
for x in name:
sum = sum + ord(x)
print(sum)
return sum%100 # number between 0 and 99
myhash("Alan")
myhash("Sunderraman")
myhash("America")
ord('S') + ord('u')