shithub: rott

Download patch

ref: f25026ddbe02ed4a03f51ff5bb9dd51caa3dd807
parent: d0a49f42e8b88797f63b02c45b00c461f62a70a6
author: LTCHIPS <ltchips994@gmail.com>
date: Thu Apr 26 13:54:59 EDT 2018

...modified HashTable even more

--- a/rott/HashTable.c
+++ b/rott/HashTable.c
@@ -13,10 +13,30 @@
     hashTable->totalSize = initSize;
     
     hashTable->table = calloc(sizeof(listNode), initSize);
+    
+    listNode * emptyNode = malloc(sizeof(listNode));
+    
+    emptyNode->isAvaliable = 1;
+    
+    emptyNode->key = -1;
+    emptyNode->data = -1;
+    
+    int x;
+    
+    for(x = 0; x < initSize; x++)
+    {
+        listNode * node = malloc(sizeof(listNode));
+        
+        memcpy(node, emptyNode, sizeof(&emptyNode));
+        
+        hashTable->table[x] = node;
+
+    }
+    
     //hashTable->table = malloc(sizeof(LinkedList) * initSize);
     
-    //memset(hashTable->table,0, sizeof(LinkedList) * initSize);
-    memset(hashTable->table, NULL, sizeof(listNode)*initSize);
+    //memset(hashTable->table,emptyNode, sizeof(listNode) * initSize);
+    //memcpy(hashTable->table, emptyNode, sizeof(listNode)*initSize);
 }
 
 int HashFunc(HashTable * hashTable, int key)
@@ -40,7 +60,9 @@
     //LinkedList * list = hashTable->table[index];
     
     //DeleteWithKey(list, key);
-    free(&hashTable->table[index]);
+    //free(&hashTable->table[index]);
+    hashTable->table[index]->isAvaliable = 0;
+    
 }
 
 void ClearHashTable (HashTable * hashTable)
@@ -63,7 +85,7 @@
     int index = HashFunc(hashTable,key);
     
     
-    while(hashTable->table[index] != NULL) 
+    while(hashTable->table[index]->isAvaliable == 0) 
     {
       //go to next cell
         ++index;
@@ -72,13 +94,12 @@
         index %= hashTable->totalSize;
     }
     
-    listNode * newNode = malloc(sizeof(listNode));
+    //listNode * newNode = malloc(sizeof(listNode));
     
-    newNode->data = item;
-    newNode->key = key;
-	
-    hashTable->table[index] = newNode;
-
+    hashTable->table[index]->data = item;
+    hashTable->table[index]->key = key;
+    hashTable->table[index]->isAvaliable = 0;
+    
 }
 
 int Lookup(HashTable * hashTable, int key)
@@ -89,12 +110,12 @@
     
     
     //If it starts off at a NULL spot, it never existed...
-    if (hashTable->table[index] == NULL)
+    if (hashTable->table[index] == 0)
     {
         return 0;
     }
     
-    while(hashTable->table[index]->key != key) 
+    while(hashTable->table[index]->key != key)
     {
       //go to next cell
         ++index;
--- a/rott/HashTable.h
+++ b/rott/HashTable.h
@@ -11,6 +11,7 @@
 
 typedef struct listNode 
 {
+    int isAvaliable;
     int key;
     int data;
 } listNode;