Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
class Listnode:
def __init__(self,key=None):
self.value=key
self.next=None
class Hashtable:
def __init__(self):
self.t=[None for i in range(30)]
def hash(self,s):
s=list(s)
n=len(s)
sum=0
for i in range(n):
a=ord(s[i])
sum=sum+a
c=sum%30
return c
def insert(self,s):
c=self.hash(s)
if self.t[c]==None:
self.t[c]=Listnode(s)
else:
temp=Listnode(s)
temp.next=self.t[c]
self.t[c]=temp
def search(self,s):
c=self.hash(s)
if self.t[c]==None:
return False
else:
temp=self.t[c]
while temp!=None and temp.value!=s:
temp=temp.next
if temp==None:
return False
return True
def printlist(self):
for i in range(30):
if self.t[i]==None:
print("None")
else:
temp=self.t[i]
while temp!=None:
print(temp.value)
temp=temp.next
def main():
l=Hashtable()
print("enter the words")
s=[]
for i in range(4):
s[i]=input("")
for i in range(len(s)):
l.insert(s[i])
l.printlist()
l.search("cat")
if __name__=='__main__':
main()