Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
Diff: hash_table.cpp
- Revision:
- 6:c9695079521d
- Parent:
- 0:35660d7952f7
diff -r 2f34484325bc -r c9695079521d hash_table.cpp --- a/hash_table.cpp Tue Oct 12 15:26:32 2021 +0000 +++ b/hash_table.cpp Fri Nov 19 22:03:25 2021 +0000 @@ -1,6 +1,6 @@ /* - Student Name: - Date: + Student Name: Nasir Christian + Date: November 9 ======================= ECE 2035 Project 2-1: @@ -126,7 +126,12 @@ * @return The pointer to the hash table entry */ static HashTableEntry* createHashTableEntry(unsigned int key, void* value) { + HashTableEntry* newEntry = (HashTableEntry*)malloc(sizeof(HashTableEntry)); + newEntry->value = value; + newEntry->key = key; + newEntry->next = NULL; + return newEntry; } /** @@ -140,8 +145,26 @@ * @return The pointer to the hash table entry, or NULL if key does not exist */ static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) { + /*Checking if hashtable is empty or not*/ + if (hashTable->num_buckets == 0) { + return NULL; + } + unsigned int index = hashTable->hash(key); + HashTableEntry* temp = hashTable->buckets[index]; + + while (temp){ + if (temp->key == key){ + return temp; + } + temp = temp->next; + } + return NULL; + } + + + + -} /**************************************************************************** * Public Interface Functions @@ -167,7 +190,7 @@ newTable->num_buckets = numBuckets; newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*)); - // As the new buckets are empty, init each bucket as NULL. + // As the new buckets contain indeterminant values, init each bucket as NULL. unsigned int i; for (i=0; i<numBuckets; ++i) { newTable->buckets[i] = NULL; @@ -178,21 +201,95 @@ } void destroyHashTable(HashTable* hashTable) { + + HashTableEntry* temp; + if (hashTable->num_buckets == 0) { + printf("Hash table has to contain at least 1 bucket...\n"); + exit(1); + } + unsigned int i; + for ( i = 0; i < hashTable->num_buckets; i++){ + HashTableEntry* point = hashTable->buckets[i]; + while(point != NULL){ + free(point->value); + temp = point->next; + free(point); + point = temp; + } + } + free(hashTable->buckets); + free(hashTable); } void* insertItem(HashTable* hashTable, unsigned int key, void* value) { + void* oldval; + HashTableEntry* entry = findItem(hashTable,key); + if (entry && (entry->key == key)){ + if(entry->value == value){ + return NULL; + } + oldval = entry->value; + entry->value = value; + return oldval; + } + else{ + HashTableEntry* newentry = createHashTableEntry(key,value); + newentry->next = hashTable->buckets[hashTable->hash(key)]; + hashTable->buckets[hashTable->hash(key)] = newentry; + return NULL; + } } void* getItem(HashTable* hashTable, unsigned int key) { + if (hashTable->num_buckets == 0) { + return NULL; + } + + HashTableEntry* me = findItem(hashTable,key); + if (me != NULL){ + return me->value; + } + else{ + return NULL; + } } void* removeItem(HashTable* hashTable, unsigned int key) { + int keySpot = hashTable->hash(key); + HashTableEntry* itemRemove = hashTable->buckets[keySpot]; + if(itemRemove && ((itemRemove->key) == key)){ + void* oldVal = itemRemove->value; + hashTable->buckets[keySpot] = itemRemove->next; + free(itemRemove); + return oldVal; + } + + if (itemRemove){ + HashTableEntry* tracker = itemRemove; + + while(itemRemove->next){ + if (((itemRemove->next)->key) == key) + { + void* old_Val = (itemRemove->next)->value; + tracker = itemRemove->next; + itemRemove->next = (tracker->next); + free(tracker); + return old_Val; + } + itemRemove = itemRemove->next; + } + } + return NULL; } + + void deleteItem(HashTable* hashTable, unsigned int key) { - -} \ No newline at end of file + /* Remove the item from the Hash Table and then free the vlaue returned*/ + void* remove = removeItem(hashTable,key); + free(remove); +}