Last active
May 19, 2018 21:15
-
-
Save scriptpapi/79c870cad0ac6b0087a998756272fd7a to your computer and use it in GitHub Desktop.
A simple hash table implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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