// HashTable_Test2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #define SIZE_KEY 32 #define SIZE_VALUE 256 #define DEFAULT_TABLESIZE 101 using namespace std; typedef struct METADATA { struct METADATA(char *key, string value) { strcpy(this->key, key); this->value = value; next = NULL; } char key[SIZE_KEY]; string value; struct METADATA *next; }METADATA; class Hashtable { private: int tablesize; METADATA** table; int size; long hashstring(char* key); METADATA* current_entry; METADATA* find(char* key); int current_index; public: Hashtable(int tablesize = DEFAULT_TABLESIZE); virtual ~Hashtable(); bool put(char* key, string value); bool get(char* key, string value); bool contains(char* key); bool remove(char* key); void removeAll(); int getSize(); void initIterator(); bool hasNext(); void getNextKey(char* key); }; void displayAll(Hashtable *hashtable); int main() { Hashtable * hashtable = new Hashtable(); char key[SIZE_KEY]; string value; strcpy(key, "389"); value = "Bob"; if(!hashtable->contains(key)) { cout << "Adding node - key: " << key << " value " << value << endl; hashtable->put(key, value); } strcpy(key, "415"); value = "Henry"; if(!hashtable->contains(key)) { cout << "Adding node - key: " << key << " value " << value << endl; hashtable->put(key, value); } strcpy(key, "999"); value = "Joe"; if(!hashtable->contains(key)) { cout << "Adding node - key: " << key << " value " << value << endl; hashtable->put(key, value); } displayAll(hashtable); return 0; } Hashtable::Hashtable(int tablesize) { size = 0; this->tablesize = tablesize; table = new METADATA*[tablesize]; for(int i = 0; inext = table[bucket]; table[bucket] = entry; size++; return true; } bool Hashtable::get(char *key, string value) { METADATA* temp = find(key); if(temp == NULL) { value = ""; return false; } else { value = temp->value; return true; } } bool Hashtable::contains(char* key) { if(find(key) == NULL) { return false; } else { return true; } } bool Hashtable::remove(char *key) { int bucket = hashstring(key); METADATA* temp = table[bucket]; if(temp == NULL) { return false; } else if(strcmp(key, temp->key) == 0) { table[bucket] = temp->next; delete temp; size--; return true; } else { METADATA* temp_next = temp->next; while(temp_next != NULL) { if(strcmp(key, temp_next->key) == 0) { temp->next = temp_next->next; delete temp_next; size--; return true; } temp= temp->next; temp_next = temp_next->next; } } return false; } void Hashtable::removeAll() { for(int i=0; inext; cout << "Removing node - key: " << temp->key << endl; delete temp; temp = next; } } } void Hashtable::getNextKey(char *key) { if(current_entry == NULL) { key[0] = '\0'; return; } strcpy(key, current_entry->key); if(current_entry->next != NULL) { current_entry = current_entry->next; } else { for(int i=current_index+1; ikey) == 0) { return temp; } temp = temp->next; } return NULL; } long Hashtable::hashstring(char *key) { int n = strlen(key); long h = 0; for(int i=0; iinitIterator(); while(hashtable->hasNext()) { hashtable->getNextKey(key); hashtable->get(key, value); cout << "key: " << key << "\tvalue : " << value << endl; } }