Still won't work

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
rconnorlawson
Date:
Fri Mar 30 17:07:25 2018 +0000
Revision:
0:35660d7952f7
Child:
2:7ae1e676b120
Hollowed out shell code.

Who changed what in which revision?

UserRevisionLine numberNew 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 */
rconnorlawson 0:35660d7952f7 128 static HashTableEntry* createHashTableEntry(unsigned int key, void* value) {
rconnorlawson 0:35660d7952f7 129
rconnorlawson 0:35660d7952f7 130 }
rconnorlawson 0:35660d7952f7 131
rconnorlawson 0:35660d7952f7 132 /**
rconnorlawson 0:35660d7952f7 133 * findItem
rconnorlawson 0:35660d7952f7 134 *
rconnorlawson 0:35660d7952f7 135 * Helper function that checks whether there exists the hash table entry that
rconnorlawson 0:35660d7952f7 136 * contains a specific key.
rconnorlawson 0:35660d7952f7 137 *
rconnorlawson 0:35660d7952f7 138 * @param hashTable The pointer to the hash table.
rconnorlawson 0:35660d7952f7 139 * @param key The key corresponds to the hash table entry
rconnorlawson 0:35660d7952f7 140 * @return The pointer to the hash table entry, or NULL if key does not exist
rconnorlawson 0:35660d7952f7 141 */
rconnorlawson 0:35660d7952f7 142 static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) {
rconnorlawson 0:35660d7952f7 143
rconnorlawson 0:35660d7952f7 144 }
rconnorlawson 0:35660d7952f7 145
rconnorlawson 0:35660d7952f7 146 /****************************************************************************
rconnorlawson 0:35660d7952f7 147 * Public Interface Functions
rconnorlawson 0:35660d7952f7 148 *
rconnorlawson 0:35660d7952f7 149 * These functions implement the public interface as specified in the header
rconnorlawson 0:35660d7952f7 150 * file, and make use of the private functions and hidden definitions in the
rconnorlawson 0:35660d7952f7 151 * above sections.
rconnorlawson 0:35660d7952f7 152 ****************************************************************************/
rconnorlawson 0:35660d7952f7 153 // The createHashTable is provided for you as a starting point.
rconnorlawson 0:35660d7952f7 154 HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) {
rconnorlawson 0:35660d7952f7 155 // The hash table has to contain at least one bucket. Exit gracefully if
rconnorlawson 0:35660d7952f7 156 // this condition is not met.
rconnorlawson 0:35660d7952f7 157 if (numBuckets==0) {
rconnorlawson 0:35660d7952f7 158 printf("Hash table has to contain at least 1 bucket...\n");
rconnorlawson 0:35660d7952f7 159 exit(1);
rconnorlawson 0:35660d7952f7 160 }
rconnorlawson 0:35660d7952f7 161
rconnorlawson 0:35660d7952f7 162 // Allocate memory for the new HashTable struct on heap.
rconnorlawson 0:35660d7952f7 163 HashTable* newTable = (HashTable*)malloc(sizeof(HashTable));
rconnorlawson 0:35660d7952f7 164
rconnorlawson 0:35660d7952f7 165 // Initialize the components of the new HashTable struct.
rconnorlawson 0:35660d7952f7 166 newTable->hash = hashFunction;
rconnorlawson 0:35660d7952f7 167 newTable->num_buckets = numBuckets;
rconnorlawson 0:35660d7952f7 168 newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*));
rconnorlawson 0:35660d7952f7 169
rconnorlawson 0:35660d7952f7 170 // As the new buckets are empty, init each bucket as NULL.
rconnorlawson 0:35660d7952f7 171 unsigned int i;
rconnorlawson 0:35660d7952f7 172 for (i=0; i<numBuckets; ++i) {
rconnorlawson 0:35660d7952f7 173 newTable->buckets[i] = NULL;
rconnorlawson 0:35660d7952f7 174 }
rconnorlawson 0:35660d7952f7 175
rconnorlawson 0:35660d7952f7 176 // Return the new HashTable struct.
rconnorlawson 0:35660d7952f7 177 return newTable;
rconnorlawson 0:35660d7952f7 178 }
rconnorlawson 0:35660d7952f7 179
rconnorlawson 0:35660d7952f7 180 void destroyHashTable(HashTable* hashTable) {
rconnorlawson 0:35660d7952f7 181
rconnorlawson 0:35660d7952f7 182 }
rconnorlawson 0:35660d7952f7 183
rconnorlawson 0:35660d7952f7 184 void* insertItem(HashTable* hashTable, unsigned int key, void* value) {
rconnorlawson 0:35660d7952f7 185
rconnorlawson 0:35660d7952f7 186 }
rconnorlawson 0:35660d7952f7 187
rconnorlawson 0:35660d7952f7 188 void* getItem(HashTable* hashTable, unsigned int key) {
rconnorlawson 0:35660d7952f7 189
rconnorlawson 0:35660d7952f7 190 }
rconnorlawson 0:35660d7952f7 191
rconnorlawson 0:35660d7952f7 192 void* removeItem(HashTable* hashTable, unsigned int key) {
rconnorlawson 0:35660d7952f7 193
rconnorlawson 0:35660d7952f7 194 }
rconnorlawson 0:35660d7952f7 195
rconnorlawson 0:35660d7952f7 196 void deleteItem(HashTable* hashTable, unsigned int key) {
rconnorlawson 0:35660d7952f7 197
rconnorlawson 0:35660d7952f7 198 }