← cproj

additional work

634679523a04a0b790027f34f20c571afafe8f25 · Ovidiu · Jun 25, 2019 15:11

+27 −4 · 3 files changed

  • mylib/include/myhash.h +1 −0
  • mylib/src/myhash.c +22 −1
  • mylib/test/t_hash.c +4 −3
diff --git a/mylib/include/myhash.h b/mylib/include/myhash.h
index abe2ea6..45a8a13 100644
--- a/mylib/include/myhash.h
+++ b/mylib/include/myhash.h
@@ -21,3 +21,4 @@ typedef struct  {
 extern HashNode *h_insert (HashTable *hTable, char *key, char *value);
 extern HashTable *h_create ( unsigned int size );
 extern HashNode *h_search (HashTable *hTable, char *key);
+extern void h_delete (HashTable *hTable, char *key);
diff --git a/mylib/src/myhash.c b/mylib/src/myhash.c
index babffbc..12f0c2a 100644
--- a/mylib/src/myhash.c
+++ b/mylib/src/myhash.c
@@ -65,7 +65,7 @@ HashTable *h_create ( unsigned int size ) {
 	return hash;
 }
 // Find a key in a given linked list
-
+// called by h_insert and h_search and h_delete
 static HashNode *h_findKey(HashNode *hNode, char *key) {
 
 	while (hNode != NULL) {
@@ -79,6 +79,7 @@ static HashNode *h_findKey(HashNode *hNode, char *key) {
 
 // Insert a key-value pair into a hash table
 
+
 HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 	HashNode *newNode;
 	// result hash will give same index and that index will already have
@@ -87,6 +88,8 @@ HashNode *h_insert (HashTable *hTable, char *key, char *value) {
 
 	newNode = hTable->h_items[resultHash];
 	// overwrites pointer if two key are same. Collisions are overwritten
+	// todo: allow for hash collisions by pushing to the next value
+
 	// cannot use realloc (save old pointer if this is the case)
 	//HashNode *oldNodePtr = newNode;
 	newNode = h_findKey(newNode, key);
@@ -135,3 +138,21 @@ HashNode *h_search (HashTable *hTable, char *key) {
 	// return the pointer
 	return newNode;
 }
+/*  
+ * Deletes a hashNode* 
+ * params: - key, hashTable
+ */
+//    #include <string.h>
+
+//        void *memset(void *s, int c, size_t n);
+
+void h_delete (HashTable *hTable, char *key) {
+
+	HashNode *freeNode;
+	freeNode = h_search(hTable,key);
+	//fprintf(stderr, "%d", sizeof(freeNode));
+	//free(freeNode->h_key);
+	//free(freeNode->h_value);
+	freeNode->h_key = (HashNode *) memset((HashNode *)freeNode, 0, sizeof(freeNode->h_key));
+	freeNode->h_value =  (HashNode *) memset((HashNode *)freeNode, 0, sizeof(freeNode->h_value));
+}
diff --git a/mylib/test/t_hash.c b/mylib/test/t_hash.c
index 6e65924..d9177f6 100644
--- a/mylib/test/t_hash.c
+++ b/mylib/test/t_hash.c
@@ -53,12 +53,13 @@ int main(int argc, char** argv){
 	assert(nodeResult != NULL);
 
 	HashNode  *nodeResult2 = h_insert(tableResult, "401", "102");
-
 	HashNode  *nodeResult3 = h_insert(tableResult, "400", "1034444444444444");
-
+	dumpTable(tableResult);
+	printf("________________\n");
 	HashNode *searchResult = h_search(tableResult,"400");
-
+	h_delete(tableResult,"400");
 	dumpTable(tableResult);
+
 #if 0
 	int i = 0;