//This function retrieves the data for the passed key and returns it //via a passed struct. It returns false if the item does not exist in the list int tableType::retrieve(char key[], commandType & foundCommand) { int targetIndex = hash(key); //Calculates the target index int success = 0; //Determines if the function was successfull if(!hashTable[targetIndex]) //The item could not be found because return 0; //there is nothing indexed at that position if(!strcmp(hashTable[targetIndex]->key,key)) //Check to see if it is the first node (head) { copyCommands(foundCommand, hashTable[targetIndex]->command); //Copies the wanted data //into the passed array success = 1; } else //Not at head { node * current = hashTable[targetIndex]; while(current) //While current is still pointing to valid data { if(!strcmp(current->key,key)) //If the keys match { copyCommands(foundCommand, current->command); //Copy desired data success = 1; } current = current->next; //Traversal, to continue searching } } return success; }
void initBluetooth(void) { initFlowControl(); copyCheckAnswers(); copyCommands(); createMainTimer(); }
//This function inserts the passed information into the ADT //It uses the hash function to determine the correct index, then places //the data there, chaining if necessary to account for collisions int tableType::insert(char key[], const commandType & newCommand) { int targetIndex = hash(key); int len = strlen(key); //Measures the key // cout << "--Target Index: " << targetIndex << endl; //Debugging //Add at the head node * temp = hashTable[targetIndex]; hashTable[targetIndex] = new node; hashTable[targetIndex]->next = temp; hashTable[targetIndex]->key = new char[len + 1]; strcpy(hashTable[targetIndex]->key, key); copyCommands(hashTable[targetIndex]->command, newCommand); //Increment Efficiency Counter ++counter[targetIndex]; //Increments the counter that tracks //the number of items in the individual chains return 1; }
void initCommandsAndAnswers(void) { copyCheckAnswers(); copyCommands(); }