// Lab Problem 9.1 // CS211 ADS2 // T Naughton, CS NUIM // 28 Apr 2000 #include #include // for strcmp() const int ISBNSize = 11; // one more than required (extra space for \0) const int TitleSize = 50; enum EntryType {USED, EMPTY, DELETED}; class HashEntry { public: char isbn[ISBNSize]; // the key char title[TitleSize]; EntryType status; }; class HashTable { int HTableSize; // should make this a static HashEntry * Entry; public: HashTable(); HashTable(int tablesize); ~HashTable(); // Methods that acts on HTable void AddEntry(char newisbn[ISBNSize], char newtitle[TitleSize]); void Print(); void Clear(); int CountFree(); void Remove(char rmisbn[ISBNSize]); private: int Hash(char isbn[ISBNSize]); }; HashTable::HashTable(){ // Initialise values HTableSize = 10; Entry = new HashEntry[HTableSize]; Clear(); } HashTable::HashTable(int tablesize){ HTableSize = tablesize; Entry = new HashEntry[HTableSize]; Clear(); } HashTable::~HashTable(){ delete [] Entry; } void HashTable::AddEntry(char newisbn[ISBNSize], char newtitle[TitleSize]){ int count; int index; if (CountFree() < 1) { cout << endl << "Table is full!"; } else { index = Hash(newisbn); while (Entry[index].status != EMPTY) { index = (index + 1) % HTableSize; } count = 0; while (count < ISBNSize) { Entry[index].isbn[count] = newisbn[count]; count++; } count = 0; while (count < TitleSize) { Entry[index].title[count] = newtitle[count]; count++; } Entry[index].status = USED; } } void HashTable::Print(){ int count; cout << endl << "Table Contents (Free = " << CountFree() << "):"; count = 0; while (count < HTableSize) { cout << "\n " << count << '\t'; switch (Entry[count].status) { case USED : cout << "USED " << Entry[count].isbn << ' '; cout << Entry[count].title; break; case EMPTY : // don't print out rubbish cout << "EMPTY "; break; case DELETED : cout << "DELETED " << Entry[count].isbn << ' '; cout << Entry[count].title; break; default: cout << "??????? "; } count++; } } void HashTable::Clear(){ int count; count = 0; while (count < HTableSize) { Entry[count].status = EMPTY; count++; } } int HashTable::CountFree(){ int count; int free; free = 0; count = 0; while (count < HTableSize) { if (Entry[count].status == EMPTY) { free++; } count++; } return free; } void HashTable::Remove(char rmisbn[ISBNSize]){ int hashindex; int count; hashindex = Hash(rmisbn); count = 0; while ((count < HTableSize) && (strcmp(rmisbn, Entry[hashindex].isbn) != 0) && (Entry[hashindex].status != EMPTY)) { hashindex = (hashindex + 1) % HTableSize; count++; } if (strcmp(rmisbn, Entry[hashindex].isbn) == 0) { Entry[hashindex].status = DELETED; } else { cout << endl << "Item " << rmisbn << " not found."; } } int HashTable::Hash(char isbn[ISBNSize]){ return int(isbn[0]) % HTableSize; } void main(void){ HashTable books(3); cout << endl << endl; books.AddEntry("1111111111\0", "Book A"); books.AddEntry("2222222222\0", "Book B"); books.Print(); books.AddEntry("999999999X\0", "Book C"); books.AddEntry("3434343434\0", "Book D"); books.AddEntry("4444444444\0", "Book E"); books.Print(); books.Remove("2222222222\0"); books.Remove("3333333333\0"); books.Print(); cout << endl << "Calling Clear()..."; books.Clear(); books.AddEntry("9876543210\0", "Book F"); books.Print(); }