/**************************************************************** * Engineering 4892 Data Structures * Assignment: 6 Date Due: Wed Aug 1st/2001 * Name: Daryl Martin * Student #: 9713520 * Username: darylm *****************************************************************/ #include "test6.cpp" /***************************************************************** * insert - Inserts r into the Hash Table. * Post: Result = Ok <-> r is insterted into the table /\ * Result = DuplicateElement <-> r already existed in the table * *****************************************************************/ template Status HashTable::insert(const Record& r) { err = Ok; //Status is OK int probe = hash(r); if((table[probe].empty()) { //If there is no values at table[probe] table[probe].push_back(r); //then we can pop r onto the list. } else { //beginning of the list we want to search through with count std::list::Iterator count = table[rinsert].begin(); //While we are not at the end of the list and we have no err. while ((count != table[probe].end()) && (err != DuplicateElement)){ if (*count == r){ err = DuplicateElement; //2 of the same values exist } else { count++; //increment to the next position } } } if (err != DublicateElement){ //if we never recieved an error we can table[rinsert].push_back(r);//push a value onto the list } return err; //return err } /***************************************************************** * remove - Remove r from the table if it exists, * otherwise set err to NoSuchElement. * * Post: Result = Ok <-> (the element, d, in the table * such that hash(r) == hash(d) /\ d == r is removed) * Result = NoSuchElement <-> No such element exists. * *****************************************************************/ template Status HashTable::remove(const Record& r) { err = Ok; //Initially err = Ok int probe = hash(r); if((table[probe].empty()) { //if the list is empty then err = NoSuchElement; //there is NoSuchElement } else { //beginning of the list we want to search through with count std::list::Iterator count = table[rinsert].begin(); bool found = false; //bool value to ease our searching //while we are still in the list and the value isn't found while ((count != table[probe].end()) && (!found)){ if (*count == r){ found == true; //value is found hence found == true } else { count++; //increment through the list } } } if (found == true){ //if found = true then we can simply pop table[probe].pop(r);//pop the value off the list } else { err = NoSuchElement;//No such element in the list } return err; //return error } /***************************************************************** * retrieve - Determine if an element == r is in the table and * assign it to r if it exists, otherwise set err to * NoSuchElement. * * Post: Result = Ok <-> (r is assigned the value stored * in the table such that hash(r) == hash(r') /\ r' == r) * Result = NoSuchElement <-> No such element exists.* *****************************************************************/ template Status HashTable::retrieve(Record& r) const { err = Ok; //Initially err = Ok int probe = hash(r); if((table[probe].empty() && r != 0) {//if the list is empty and r is err = NoSuchElement; //not empty than NoSuchElement } else { //beginning of the list we want to search through with count std::list::Iterator count = table[rinsert].begin(); bool found = false; //bool value to ease our search while ((count != table[probe].end()) && (!found)){ if (*count == r){ found == true; //we found our value } else { count++; //increment through the list } } } if (found == true){ //if we found our value then r = table[probe]; //let r == the probe value } else { err = NoSuchElement; //else there is no such element } return err; //return error value }