Skip to content

Instantly share code, notes, and snippets.

@scriptpapi
Last active May 19, 2018 21:15
Show Gist options
  • Save scriptpapi/79c870cad0ac6b0087a998756272fd7a to your computer and use it in GitHub Desktop.
Save scriptpapi/79c870cad0ac6b0087a998756272fd7a to your computer and use it in GitHub Desktop.
A simple hash table implementation
# A simple of implementation of a hash table
class Node:
def __init__(self, key, newData):
self.key = key
self.data = newData
def getKey(self):
return self.key
def getData(self):
return self.data
class Hash:
def __init__(self, capacity):
self.hTable = [None]*capacity
def hashFunc(self, newData):
return newData % 21
def insert(self, key, newData):
self.hTable[self.hashFunc(key)] = Node(key, newData)
def find(self, key):
if self.hTable[self.hashFunc(key)]:
tmp = self.hTable[self.hashFunc(key)]
while tmp:
if key == tmp.getKey():
return tmp.getData()
tmp = tmp.next
return False
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment