Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
hash_table.cpp@4:2297a714936f, 2019-11-19 (annotated)
- Committer:
- trich9
- Date:
- Tue Nov 19 16:53:47 2019 +0000
- Revision:
- 4:2297a714936f
- Parent:
- 0:35660d7952f7
11/19/2019;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rconnorlawson | 0:35660d7952f7 | 1 | /* |
| rconnorlawson | 0:35660d7952f7 | 2 | Student Name: |
| rconnorlawson | 0:35660d7952f7 | 3 | Date: |
| rconnorlawson | 0:35660d7952f7 | 4 | |
| rconnorlawson | 0:35660d7952f7 | 5 | ======================= |
| rconnorlawson | 0:35660d7952f7 | 6 | ECE 2035 Project 2-1: |
| rconnorlawson | 0:35660d7952f7 | 7 | ======================= |
| rconnorlawson | 0:35660d7952f7 | 8 | This file provides definition for the structs and functions declared in the |
| rconnorlawson | 0:35660d7952f7 | 9 | header file. It also contains helper functions that are not accessible from |
| rconnorlawson | 0:35660d7952f7 | 10 | outside of the file. |
| rconnorlawson | 0:35660d7952f7 | 11 | |
| rconnorlawson | 0:35660d7952f7 | 12 | FOR FULL CREDIT, BE SURE TO TRY MULTIPLE TEST CASES and DOCUMENT YOUR CODE. |
| rconnorlawson | 0:35660d7952f7 | 13 | |
| rconnorlawson | 0:35660d7952f7 | 14 | =================================== |
| rconnorlawson | 0:35660d7952f7 | 15 | Naming conventions in this file: |
| rconnorlawson | 0:35660d7952f7 | 16 | =================================== |
| rconnorlawson | 0:35660d7952f7 | 17 | 1. All struct names use camel case where the first letter is capitalized. |
| rconnorlawson | 0:35660d7952f7 | 18 | e.g. "HashTable", or "HashTableEntry" |
| rconnorlawson | 0:35660d7952f7 | 19 | |
| rconnorlawson | 0:35660d7952f7 | 20 | 2. Variable names with a preceding underscore "_" will not be called directly. |
| rconnorlawson | 0:35660d7952f7 | 21 | e.g. "_HashTable", "_HashTableEntry" |
| rconnorlawson | 0:35660d7952f7 | 22 | |
| rconnorlawson | 0:35660d7952f7 | 23 | Recall that in C, we have to type "struct" together with the name of the struct |
| rconnorlawson | 0:35660d7952f7 | 24 | in order to initialize a new variable. To avoid this, in hash_table.h |
| rconnorlawson | 0:35660d7952f7 | 25 | we use typedef to provide new "nicknames" for "struct _HashTable" and |
| rconnorlawson | 0:35660d7952f7 | 26 | "struct _HashTableEntry". As a result, we can create new struct variables |
| rconnorlawson | 0:35660d7952f7 | 27 | by just using: |
| rconnorlawson | 0:35660d7952f7 | 28 | - "HashTable myNewTable;" |
| rconnorlawson | 0:35660d7952f7 | 29 | or |
| rconnorlawson | 0:35660d7952f7 | 30 | - "HashTableEntry myNewHashTableEntry;" |
| rconnorlawson | 0:35660d7952f7 | 31 | |
| rconnorlawson | 0:35660d7952f7 | 32 | The preceding underscore "_" simply provides a distinction between the names |
| rconnorlawson | 0:35660d7952f7 | 33 | of the actual struct defition and the "nicknames" that we use to initialize |
| rconnorlawson | 0:35660d7952f7 | 34 | new structs. |
| rconnorlawson | 0:35660d7952f7 | 35 | [See Hidden Definitions section for more information.] |
| rconnorlawson | 0:35660d7952f7 | 36 | |
| rconnorlawson | 0:35660d7952f7 | 37 | 3. Functions, their local variables and arguments are named with camel case, where |
| rconnorlawson | 0:35660d7952f7 | 38 | the first letter is lower-case. |
| rconnorlawson | 0:35660d7952f7 | 39 | e.g. "createHashTable" is a function. One of its arguments is "numBuckets". |
| rconnorlawson | 0:35660d7952f7 | 40 | It also has a local variable called "newTable". |
| rconnorlawson | 0:35660d7952f7 | 41 | |
| rconnorlawson | 0:35660d7952f7 | 42 | 4. The name of a struct member is divided by using underscores "_". This serves |
| rconnorlawson | 0:35660d7952f7 | 43 | as a distinction between function local variables and struct members. |
| rconnorlawson | 0:35660d7952f7 | 44 | e.g. "num_buckets" is a member of "HashTable". |
| rconnorlawson | 0:35660d7952f7 | 45 | |
| rconnorlawson | 0:35660d7952f7 | 46 | */ |
| rconnorlawson | 0:35660d7952f7 | 47 | |
| rconnorlawson | 0:35660d7952f7 | 48 | /**************************************************************************** |
| rconnorlawson | 0:35660d7952f7 | 49 | * Include the Public Interface |
| rconnorlawson | 0:35660d7952f7 | 50 | * |
| rconnorlawson | 0:35660d7952f7 | 51 | * By including the public interface at the top of the file, the compiler can |
| rconnorlawson | 0:35660d7952f7 | 52 | * enforce that the function declarations in the the header are not in |
| rconnorlawson | 0:35660d7952f7 | 53 | * conflict with the definitions in the file. This is not a guarantee of |
| rconnorlawson | 0:35660d7952f7 | 54 | * correctness, but it is better than nothing! |
| rconnorlawson | 0:35660d7952f7 | 55 | ***************************************************************************/ |
| rconnorlawson | 0:35660d7952f7 | 56 | #include "hash_table.h" |
| rconnorlawson | 0:35660d7952f7 | 57 | |
| rconnorlawson | 0:35660d7952f7 | 58 | |
| rconnorlawson | 0:35660d7952f7 | 59 | /**************************************************************************** |
| rconnorlawson | 0:35660d7952f7 | 60 | * Include other private dependencies |
| rconnorlawson | 0:35660d7952f7 | 61 | * |
| rconnorlawson | 0:35660d7952f7 | 62 | * These other modules are used in the implementation of the hash table module, |
| rconnorlawson | 0:35660d7952f7 | 63 | * but are not required by users of the hash table. |
| rconnorlawson | 0:35660d7952f7 | 64 | ***************************************************************************/ |
| rconnorlawson | 0:35660d7952f7 | 65 | #include <stdlib.h> // For malloc and free |
| rconnorlawson | 0:35660d7952f7 | 66 | #include <stdio.h> // For printf |
| rconnorlawson | 0:35660d7952f7 | 67 | |
| rconnorlawson | 0:35660d7952f7 | 68 | |
| rconnorlawson | 0:35660d7952f7 | 69 | /**************************************************************************** |
| rconnorlawson | 0:35660d7952f7 | 70 | * Hidden Definitions |
| rconnorlawson | 0:35660d7952f7 | 71 | * |
| rconnorlawson | 0:35660d7952f7 | 72 | * These definitions are not available outside of this file. However, because |
| rconnorlawson | 0:35660d7952f7 | 73 | * the are forward declared in hash_table.h, the type names are |
| rconnorlawson | 0:35660d7952f7 | 74 | * available everywhere and user code can hold pointers to these structs. |
| rconnorlawson | 0:35660d7952f7 | 75 | ***************************************************************************/ |
| rconnorlawson | 0:35660d7952f7 | 76 | /** |
| rconnorlawson | 0:35660d7952f7 | 77 | * This structure represents an a hash table. |
| rconnorlawson | 0:35660d7952f7 | 78 | * Use "HashTable" instead when you are creating a new variable. [See top comments] |
| rconnorlawson | 0:35660d7952f7 | 79 | */ |
| rconnorlawson | 0:35660d7952f7 | 80 | struct _HashTable { |
| rconnorlawson | 0:35660d7952f7 | 81 | /** The array of pointers to the head of a singly linked list, whose nodes |
| rconnorlawson | 0:35660d7952f7 | 82 | are HashTableEntry objects */ |
| rconnorlawson | 0:35660d7952f7 | 83 | HashTableEntry** buckets; |
| rconnorlawson | 0:35660d7952f7 | 84 | |
| rconnorlawson | 0:35660d7952f7 | 85 | /** The hash function pointer */ |
| rconnorlawson | 0:35660d7952f7 | 86 | HashFunction hash; |
| rconnorlawson | 0:35660d7952f7 | 87 | |
| rconnorlawson | 0:35660d7952f7 | 88 | /** The number of buckets in the hash table */ |
| rconnorlawson | 0:35660d7952f7 | 89 | unsigned int num_buckets; |
| rconnorlawson | 0:35660d7952f7 | 90 | }; |
| rconnorlawson | 0:35660d7952f7 | 91 | |
| rconnorlawson | 0:35660d7952f7 | 92 | /** |
| rconnorlawson | 0:35660d7952f7 | 93 | * This structure represents a hash table entry. |
| rconnorlawson | 0:35660d7952f7 | 94 | * Use "HashTableEntry" instead when you are creating a new variable. [See top comments] |
| rconnorlawson | 0:35660d7952f7 | 95 | */ |
| rconnorlawson | 0:35660d7952f7 | 96 | struct _HashTableEntry { |
| rconnorlawson | 0:35660d7952f7 | 97 | /** The key for the hash table entry */ |
| rconnorlawson | 0:35660d7952f7 | 98 | unsigned int key; |
| rconnorlawson | 0:35660d7952f7 | 99 | |
| rconnorlawson | 0:35660d7952f7 | 100 | /** The value associated with this hash table entry */ |
| rconnorlawson | 0:35660d7952f7 | 101 | void* value; |
| rconnorlawson | 0:35660d7952f7 | 102 | |
| rconnorlawson | 0:35660d7952f7 | 103 | /** |
| rconnorlawson | 0:35660d7952f7 | 104 | * A pointer pointing to the next hash table entry |
| rconnorlawson | 0:35660d7952f7 | 105 | * NULL means there is no next entry (i.e. this is the tail) |
| rconnorlawson | 0:35660d7952f7 | 106 | */ |
| rconnorlawson | 0:35660d7952f7 | 107 | HashTableEntry* next; |
| rconnorlawson | 0:35660d7952f7 | 108 | }; |
| rconnorlawson | 0:35660d7952f7 | 109 | |
| rconnorlawson | 0:35660d7952f7 | 110 | |
| rconnorlawson | 0:35660d7952f7 | 111 | /**************************************************************************** |
| rconnorlawson | 0:35660d7952f7 | 112 | * Private Functions |
| rconnorlawson | 0:35660d7952f7 | 113 | * |
| rconnorlawson | 0:35660d7952f7 | 114 | * These functions are not available outside of this file, since they are not |
| rconnorlawson | 0:35660d7952f7 | 115 | * declared in hash_table.h. |
| rconnorlawson | 0:35660d7952f7 | 116 | ***************************************************************************/ |
| rconnorlawson | 0:35660d7952f7 | 117 | /** |
| rconnorlawson | 0:35660d7952f7 | 118 | * createHashTableEntry |
| rconnorlawson | 0:35660d7952f7 | 119 | * |
| rconnorlawson | 0:35660d7952f7 | 120 | * Helper function that creates a hash table entry by allocating memory for it on |
| rconnorlawson | 0:35660d7952f7 | 121 | * the heap. It initializes the entry with key and value, initialize pointer to |
| rconnorlawson | 0:35660d7952f7 | 122 | * the next entry as NULL, and return the pointer to this hash table entry. |
| rconnorlawson | 0:35660d7952f7 | 123 | * |
| rconnorlawson | 0:35660d7952f7 | 124 | * @param key The key corresponds to the hash table entry |
| rconnorlawson | 0:35660d7952f7 | 125 | * @param value The value stored in the hash table entry |
| rconnorlawson | 0:35660d7952f7 | 126 | * @return The pointer to the hash table entry |
| rconnorlawson | 0:35660d7952f7 | 127 | */ |
| trich9 | 4:2297a714936f | 128 | //my code for createHashTableEntry |
| rconnorlawson | 0:35660d7952f7 | 129 | static HashTableEntry* createHashTableEntry(unsigned int key, void* value) { |
| trich9 | 4:2297a714936f | 130 | |
| trich9 | 4:2297a714936f | 131 | HashTableEntry *newHashTableEntry; |
| trich9 | 4:2297a714936f | 132 | |
| trich9 | 4:2297a714936f | 133 | newHashTableEntry = (HashTableEntry*) malloc(sizeof(HashTableEntry));//malloc space |
| trich9 | 4:2297a714936f | 134 | if(!newHashTableEntry){ |
| trich9 | 4:2297a714936f | 135 | return NULL; |
| trich9 | 4:2297a714936f | 136 | } |
| trich9 | 4:2297a714936f | 137 | |
| trich9 | 4:2297a714936f | 138 | newHashTableEntry -> key = key; //assign key |
| trich9 | 4:2297a714936f | 139 | newHashTableEntry -> value = value; //assign value |
| trich9 | 4:2297a714936f | 140 | |
| trich9 | 4:2297a714936f | 141 | |
| trich9 | 4:2297a714936f | 142 | return newHashTableEntry; //return the address of the HashTableEntry |
| rconnorlawson | 0:35660d7952f7 | 143 | } |
| rconnorlawson | 0:35660d7952f7 | 144 | |
| rconnorlawson | 0:35660d7952f7 | 145 | /** |
| rconnorlawson | 0:35660d7952f7 | 146 | * findItem |
| rconnorlawson | 0:35660d7952f7 | 147 | * |
| rconnorlawson | 0:35660d7952f7 | 148 | * Helper function that checks whether there exists the hash table entry that |
| rconnorlawson | 0:35660d7952f7 | 149 | * contains a specific key. |
| rconnorlawson | 0:35660d7952f7 | 150 | * |
| rconnorlawson | 0:35660d7952f7 | 151 | * @param hashTable The pointer to the hash table. |
| rconnorlawson | 0:35660d7952f7 | 152 | * @param key The key corresponds to the hash table entry |
| rconnorlawson | 0:35660d7952f7 | 153 | * @return The pointer to the hash table entry, or NULL if key does not exist |
| rconnorlawson | 0:35660d7952f7 | 154 | */ |
| trich9 | 4:2297a714936f | 155 | //myCode |
| rconnorlawson | 0:35660d7952f7 | 156 | static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) { |
| trich9 | 4:2297a714936f | 157 | |
| trich9 | 4:2297a714936f | 158 | HashTableEntry* this_node = hashTable -> buckets[hashTable->hash(key)];//head |
| trich9 | 4:2297a714936f | 159 | while(this_node){//while there is a next node |
| trich9 | 4:2297a714936f | 160 | if(this_node -> key == key){//if the nodes key is equal to one we are searching for |
| trich9 | 4:2297a714936f | 161 | return this_node; |
| trich9 | 4:2297a714936f | 162 | } |
| trich9 | 4:2297a714936f | 163 | this_node = this_node -> next;//goes to next node |
| trich9 | 4:2297a714936f | 164 | } |
| trich9 | 4:2297a714936f | 165 | return NULL;//returns null if not found |
| rconnorlawson | 0:35660d7952f7 | 166 | } |
| rconnorlawson | 0:35660d7952f7 | 167 | |
| rconnorlawson | 0:35660d7952f7 | 168 | /**************************************************************************** |
| rconnorlawson | 0:35660d7952f7 | 169 | * Public Interface Functions |
| rconnorlawson | 0:35660d7952f7 | 170 | * |
| rconnorlawson | 0:35660d7952f7 | 171 | * These functions implement the public interface as specified in the header |
| rconnorlawson | 0:35660d7952f7 | 172 | * file, and make use of the private functions and hidden definitions in the |
| rconnorlawson | 0:35660d7952f7 | 173 | * above sections. |
| rconnorlawson | 0:35660d7952f7 | 174 | ****************************************************************************/ |
| rconnorlawson | 0:35660d7952f7 | 175 | // The createHashTable is provided for you as a starting point. |
| rconnorlawson | 0:35660d7952f7 | 176 | HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) { |
| rconnorlawson | 0:35660d7952f7 | 177 | // The hash table has to contain at least one bucket. Exit gracefully if |
| rconnorlawson | 0:35660d7952f7 | 178 | // this condition is not met. |
| rconnorlawson | 0:35660d7952f7 | 179 | if (numBuckets==0) { |
| rconnorlawson | 0:35660d7952f7 | 180 | printf("Hash table has to contain at least 1 bucket...\n"); |
| rconnorlawson | 0:35660d7952f7 | 181 | exit(1); |
| rconnorlawson | 0:35660d7952f7 | 182 | } |
| rconnorlawson | 0:35660d7952f7 | 183 | |
| rconnorlawson | 0:35660d7952f7 | 184 | // Allocate memory for the new HashTable struct on heap. |
| rconnorlawson | 0:35660d7952f7 | 185 | HashTable* newTable = (HashTable*)malloc(sizeof(HashTable)); |
| rconnorlawson | 0:35660d7952f7 | 186 | |
| rconnorlawson | 0:35660d7952f7 | 187 | // Initialize the components of the new HashTable struct. |
| rconnorlawson | 0:35660d7952f7 | 188 | newTable->hash = hashFunction; |
| rconnorlawson | 0:35660d7952f7 | 189 | newTable->num_buckets = numBuckets; |
| rconnorlawson | 0:35660d7952f7 | 190 | newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*)); |
| rconnorlawson | 0:35660d7952f7 | 191 | |
| trich9 | 4:2297a714936f | 192 | // As the new buckets contain indeterminant values, init each bucket as NULL. |
| rconnorlawson | 0:35660d7952f7 | 193 | unsigned int i; |
| rconnorlawson | 0:35660d7952f7 | 194 | for (i=0; i<numBuckets; ++i) { |
| rconnorlawson | 0:35660d7952f7 | 195 | newTable->buckets[i] = NULL; |
| rconnorlawson | 0:35660d7952f7 | 196 | } |
| rconnorlawson | 0:35660d7952f7 | 197 | |
| rconnorlawson | 0:35660d7952f7 | 198 | // Return the new HashTable struct. |
| rconnorlawson | 0:35660d7952f7 | 199 | return newTable; |
| rconnorlawson | 0:35660d7952f7 | 200 | } |
| rconnorlawson | 0:35660d7952f7 | 201 | |
| trich9 | 4:2297a714936f | 202 | /** |
| trich9 | 4:2297a714936f | 203 | * destroyHashTable |
| trich9 | 4:2297a714936f | 204 | * |
| trich9 | 4:2297a714936f | 205 | * Destroy the hash table. The nodes (HashTableEntry objects) of singly linked |
| trich9 | 4:2297a714936f | 206 | * list, the values stored on the linked list, the buckets, and the hashtable |
| trich9 | 4:2297a714936f | 207 | * itself are freed from the heap. In other words, free all the allocated memory |
| trich9 | 4:2297a714936f | 208 | * on heap that is associated with heap, including the values that users store in |
| trich9 | 4:2297a714936f | 209 | * the hash table. |
| trich9 | 4:2297a714936f | 210 | * |
| trich9 | 4:2297a714936f | 211 | * @param myHashTable The pointer to the hash table. |
| trich9 | 4:2297a714936f | 212 | * |
| trich9 | 4:2297a714936f | 213 | */ |
| trich9 | 4:2297a714936f | 214 | //mycode |
| trich9 | 4:2297a714936f | 215 | void destroyHashTable(HashTable* hashTable) {//destroy everything |
| trich9 | 4:2297a714936f | 216 | unsigned int numBuckets = hashTable -> num_buckets;// pull num_buckets |
| trich9 | 4:2297a714936f | 217 | //hashTable -> buckets[i]; |
| trich9 | 4:2297a714936f | 218 | for(int i = 0; i < numBuckets; i++){//loop thorugh every bucket |
| trich9 | 4:2297a714936f | 219 | HashTableEntry* this_node = hashTable -> buckets[i]; |
| trich9 | 4:2297a714936f | 220 | |
| trich9 | 4:2297a714936f | 221 | while(this_node){//loop thorugh every node in a bucket |
| trich9 | 4:2297a714936f | 222 | HashTableEntry* temp = this_node -> next; |
| trich9 | 4:2297a714936f | 223 | free(this_node -> value); |
| trich9 | 4:2297a714936f | 224 | free(this_node); |
| trich9 | 4:2297a714936f | 225 | this_node = temp; |
| trich9 | 4:2297a714936f | 226 | } |
| trich9 | 4:2297a714936f | 227 | } |
| trich9 | 4:2297a714936f | 228 | free(hashTable);//MR |
| trich9 | 4:2297a714936f | 229 | return; |
| rconnorlawson | 0:35660d7952f7 | 230 | } |
| trich9 | 4:2297a714936f | 231 | /** |
| trich9 | 4:2297a714936f | 232 | * insertItem |
| trich9 | 4:2297a714936f | 233 | * |
| trich9 | 4:2297a714936f | 234 | * Insert the value into the hash table based on the key. |
| trich9 | 4:2297a714936f | 235 | * In other words, create a new hash table entry and add it to a specific bucket. |
| trich9 | 4:2297a714936f | 236 | * |
| trich9 | 4:2297a714936f | 237 | * @param myHashTable The pointer to the hash table. |
| trich9 | 4:2297a714936f | 238 | * @param key The key that corresponds to the value. |
| trich9 | 4:2297a714936f | 239 | * @param value The value to be stored in the hash table. |
| trich9 | 4:2297a714936f | 240 | * @return old value if it is overwritten, or NULL if not replaced |
| trich9 | 4:2297a714936f | 241 | */ |
| trich9 | 4:2297a714936f | 242 | //my code |
| trich9 | 4:2297a714936f | 243 | //og had: void* insertItem(HashTable* hashTable, unsigned int key, void* value) |
| rconnorlawson | 0:35660d7952f7 | 244 | void* insertItem(HashTable* hashTable, unsigned int key, void* value) { |
| trich9 | 4:2297a714936f | 245 | HashTableEntry* this_node; |
| trich9 | 4:2297a714936f | 246 | |
| trich9 | 4:2297a714936f | 247 | if(this_node = findItem(hashTable, key)){//key in list |
| trich9 | 4:2297a714936f | 248 | void* ret_value = this_node -> value;//save value |
| trich9 | 4:2297a714936f | 249 | this_node -> value = value; //give new value |
| trich9 | 4:2297a714936f | 250 | return ret_value; //return old value |
| trich9 | 4:2297a714936f | 251 | } |
| trich9 | 4:2297a714936f | 252 | |
| trich9 | 4:2297a714936f | 253 | //key not in list |
| trich9 | 4:2297a714936f | 254 | this_node = createHashTableEntry(key, value); //create new node |
| trich9 | 4:2297a714936f | 255 | int bHash = hashTable -> hash(key); //get the index of bucket it is in |
| trich9 | 4:2297a714936f | 256 | |
| trich9 | 4:2297a714936f | 257 | this_node ->next = hashTable -> buckets[bHash]; //this_node -> next = head |
| trich9 | 4:2297a714936f | 258 | hashTable -> buckets[bHash] = this_node; //head = this_node |
| trich9 | 4:2297a714936f | 259 | return NULL; |
| trich9 | 4:2297a714936f | 260 | |
| rconnorlawson | 0:35660d7952f7 | 261 | } |
| trich9 | 4:2297a714936f | 262 | /** |
| trich9 | 4:2297a714936f | 263 | * getItem |
| trich9 | 4:2297a714936f | 264 | * |
| trich9 | 4:2297a714936f | 265 | * Get the value that corresponds to the key in the hash table. |
| trich9 | 4:2297a714936f | 266 | * |
| trich9 | 4:2297a714936f | 267 | * @param myHashTable The pointer to the hash table. |
| trich9 | 4:2297a714936f | 268 | * @param key The key that corresponds to the item. |
| trich9 | 4:2297a714936f | 269 | * @return the value corresponding to the key, or NULL if the key is not present |
| trich9 | 4:2297a714936f | 270 | */ |
| rconnorlawson | 0:35660d7952f7 | 271 | void* getItem(HashTable* hashTable, unsigned int key) { |
| trich9 | 4:2297a714936f | 272 | |
| trich9 | 4:2297a714936f | 273 | HashTableEntry* found_node = findItem(hashTable, key); //head_node = head |
| trich9 | 4:2297a714936f | 274 | |
| trich9 | 4:2297a714936f | 275 | //if it found the node return the value of it |
| trich9 | 4:2297a714936f | 276 | if(found_node){ |
| trich9 | 4:2297a714936f | 277 | return found_node -> value; |
| trich9 | 4:2297a714936f | 278 | } |
| trich9 | 4:2297a714936f | 279 | //otherwise return null |
| trich9 | 4:2297a714936f | 280 | return NULL; |
| trich9 | 4:2297a714936f | 281 | } |
| trich9 | 4:2297a714936f | 282 | /** |
| trich9 | 4:2297a714936f | 283 | * removeItem |
| trich9 | 4:2297a714936f | 284 | * |
| trich9 | 4:2297a714936f | 285 | * Remove the item in hash table based on the key and return the value stored in it. |
| trich9 | 4:2297a714936f | 286 | * In other words, return the value and free the hash table entry from heap. |
| trich9 | 4:2297a714936f | 287 | * |
| trich9 | 4:2297a714936f | 288 | * @param myHashTable The pointer to the hash table. |
| trich9 | 4:2297a714936f | 289 | * @param key The key that corresponds to the item. |
| trich9 | 4:2297a714936f | 290 | * @return the pointer of the value corresponding to the key, or NULL if the key is not present |
| trich9 | 4:2297a714936f | 291 | */ |
| trich9 | 4:2297a714936f | 292 | void* removeItem(HashTable* hashTable, unsigned int key) { |
| trich9 | 4:2297a714936f | 293 | HashTableEntry* this_node = hashTable -> buckets[hashTable->hash(key)]; //this_node = head |
| rconnorlawson | 0:35660d7952f7 | 294 | |
| trich9 | 4:2297a714936f | 295 | //case empty |
| trich9 | 4:2297a714936f | 296 | if(!this_node) |
| trich9 | 4:2297a714936f | 297 | return NULL; |
| trich9 | 4:2297a714936f | 298 | |
| trich9 | 4:2297a714936f | 299 | //case first element |
| trich9 | 4:2297a714936f | 300 | if(this_node -> key == key){ |
| trich9 | 4:2297a714936f | 301 | void* ret_value = this_node -> value;//save the value |
| trich9 | 4:2297a714936f | 302 | hashTable -> buckets[hashTable->hash(key)] = this_node -> next;//set head to the next node |
| trich9 | 4:2297a714936f | 303 | //free all components |
| trich9 | 4:2297a714936f | 304 | |
| trich9 | 4:2297a714936f | 305 | free(this_node); |
| trich9 | 4:2297a714936f | 306 | return ret_value; |
| trich9 | 4:2297a714936f | 307 | } |
| trich9 | 4:2297a714936f | 308 | |
| trich9 | 4:2297a714936f | 309 | //case need to walk the list |
| trich9 | 4:2297a714936f | 310 | while(this_node -> next){//already checked first node |
| trich9 | 4:2297a714936f | 311 | if(this_node -> next -> key == key){//delete this if true |
| trich9 | 4:2297a714936f | 312 | HashTableEntry* temp_node = this_node -> next; |
| trich9 | 4:2297a714936f | 313 | void* ret_value = temp_node -> value;//save the value |
| trich9 | 4:2297a714936f | 314 | this_node -> next = this_node -> next -> next; |
| trich9 | 4:2297a714936f | 315 | |
| trich9 | 4:2297a714936f | 316 | free(temp_node); |
| trich9 | 4:2297a714936f | 317 | return ret_value; |
| trich9 | 4:2297a714936f | 318 | |
| trich9 | 4:2297a714936f | 319 | } |
| trich9 | 4:2297a714936f | 320 | this_node = this_node -> next; |
| trich9 | 4:2297a714936f | 321 | } |
| trich9 | 4:2297a714936f | 322 | |
| trich9 | 4:2297a714936f | 323 | return NULL;//did not find anything to remove |
| rconnorlawson | 0:35660d7952f7 | 324 | } |
| trich9 | 4:2297a714936f | 325 | /** |
| trich9 | 4:2297a714936f | 326 | * deleteItem |
| trich9 | 4:2297a714936f | 327 | * |
| trich9 | 4:2297a714936f | 328 | * Delete the item in the hash table based on the key. In other words, free the |
| trich9 | 4:2297a714936f | 329 | * value stored in the hash table entry and the hash table entry itself from |
| trich9 | 4:2297a714936f | 330 | * the heap. |
| trich9 | 4:2297a714936f | 331 | * |
| trich9 | 4:2297a714936f | 332 | * @param myHashTable The pointer to the hash table. |
| trich9 | 4:2297a714936f | 333 | * @param key The key that corresponds to the item. |
| trich9 | 4:2297a714936f | 334 | * |
| trich9 | 4:2297a714936f | 335 | */ |
| trich9 | 4:2297a714936f | 336 | |
| trich9 | 4:2297a714936f | 337 | void deleteItem(HashTable* hashTable, unsigned int key){ |
| trich9 | 4:2297a714936f | 338 | HashTableEntry* this_node = hashTable -> buckets[hashTable->hash(key)]; //this_node = head |
| rconnorlawson | 0:35660d7952f7 | 339 | |
| trich9 | 4:2297a714936f | 340 | //case empty |
| trich9 | 4:2297a714936f | 341 | if(!this_node) |
| trich9 | 4:2297a714936f | 342 | return; |
| trich9 | 4:2297a714936f | 343 | |
| trich9 | 4:2297a714936f | 344 | //case first element |
| trich9 | 4:2297a714936f | 345 | if(this_node -> key == key){ |
| trich9 | 4:2297a714936f | 346 | hashTable -> buckets[hashTable->hash(key)] = this_node -> next;//set head to the next node |
| trich9 | 4:2297a714936f | 347 | //free all components |
| trich9 | 4:2297a714936f | 348 | free(this_node -> value); |
| trich9 | 4:2297a714936f | 349 | free(this_node); |
| trich9 | 4:2297a714936f | 350 | return; |
| trich9 | 4:2297a714936f | 351 | } |
| trich9 | 4:2297a714936f | 352 | |
| trich9 | 4:2297a714936f | 353 | //case need to walk the list |
| trich9 | 4:2297a714936f | 354 | while(this_node -> next){//already checked first node |
| trich9 | 4:2297a714936f | 355 | if(this_node -> next -> key == key){//delete this if true |
| trich9 | 4:2297a714936f | 356 | HashTableEntry* temp_node = this_node -> next; |
| trich9 | 4:2297a714936f | 357 | |
| trich9 | 4:2297a714936f | 358 | this_node -> next = this_node -> next -> next; |
| trich9 | 4:2297a714936f | 359 | free(temp_node->value); |
| trich9 | 4:2297a714936f | 360 | free(temp_node); |
| trich9 | 4:2297a714936f | 361 | return; |
| trich9 | 4:2297a714936f | 362 | |
| trich9 | 4:2297a714936f | 363 | } |
| trich9 | 4:2297a714936f | 364 | this_node = this_node -> next; |
| trich9 | 4:2297a714936f | 365 | } |
| trich9 | 4:2297a714936f | 366 | |
| trich9 | 4:2297a714936f | 367 | return; |
| trich9 | 4:2297a714936f | 368 | } |