Your program should meet the following requirements:
The gradescript will test all of the dlistrev-x.cpp programs from the lecture notes, plus the program list_editor that I describe below.
#include "dlist.h" #include <vector> using namespace std; class DL_Hash { public: DL_Hash(int size); ~DL_Hash(); void Insert(string &s); int Present(string &s); void Erase(string &s); void Strip_All_Substring(string &s); void Print(); protected: vector <Dlist *> table; }; |
Your job is to implement this class in dl_hash.cpp.
The hash table is going to use separate chaining, using the Dlist type to implement the chains. Please make note that each element of the hash table is a pointer to a list, so you'll have to call "new Dlist" for every element of the table in your constructor. You'll also have to call delete in the destructor.
Insert() inserts a string into the hash table. Use djb_hash() as the hash function. If the string is already there, it should do nothing. Present() returns whether the string is present in the hash table, and Erase() erases the string if it is there. If it's not there, Erase() should do nothing.
Strip_All_Substring() should delete every entry in the hash table that has the given string as a substring. You should not use the Erase() method of the DL_Hash class for this. You should use the Erase() method of the Dlist class.
Finally, Print() prints out the hash table. It does this in order of hash table entry, and for strings that have the same hash value, they are printed in the order in which the elements were inserted into the hash table. The format should be one string per line, where each line contains the hash index, padded to four characters and right justified, then a space and then the string.
To test this, I have written dl_hash_test.cpp which is a simple command-based program which supports INSERT, INSERT, PRESENT, ERASE, STRIP, PRINT and QUIT. Its command line arguments are the table size and a prompt. Using it is straightforward:
UNIX> dl_hash_test 50 'DL_Hash>' DL_Hash> INSERT Thor DL_Hash> INSERT Binky DL_Hash> INSERT Brody Masonry DL_Hash> PRESENT Thor Yes DL_Hash> PRESENT Frenchy No DL_Hash> PRINT 42 Binky 42 Brody Masonry 46 Thor DL_Hash> ERASE Thor DL_Hash> PRINT 42 Binky 42 Brody Masonry DL_Hash> ERASE Thor -- This does nothing, since Thor is no longer in the table. DL_Hash> PRINT 42 Binky 42 Brody Masonry DL_Hash> INSERT Binky -- This does nothing, since Binky is already in the table. DL_Hash> PRINT 42 Binky 42 Brody Masonry DL_Hash> INSERT Pinky DL_Hash> INSERT Dinky DL_Hash> INSERT Winky DL_Hash> PRINT 33 Winky 34 Dinky 36 Pinky 42 Binky 42 Brody Masonry DL_Hash> STRIP ink DL_Hash> PRINT 42 Brody Masonry DL_Hash> QUIT UNIX>
The following commands are supported:
UNIX> list_editor 'Editor>' Editor> PUSH_BACK a Editor> PUSH_BACK b Editor> PUSH_BACK c Editor> PUSH_BACK d Editor> PRINT_FORWARD a b c d Editor> PUSH_FRONT z Editor> PRINT_FORWARD z a b c d Editor> PRINT_REVERSE d c b a z Editor> POP_BACK d Editor> POP_FRONT z Editor> PRINT_FORWARD a b c Editor> SIZE 3 Editor> EMPTY No Editor> INSERT_BEFORE xxx b Editor> PRINT_FORWARD a xxx b c Editor> INSERT_AFTER yyy b Editor> PRINT_FORWARD a xxx b yyy c Editor> ERASE b Editor> PRINT_FORWARD a xxx yyy c Editor> SIZE 4 Editor> CLEAR Editor> EMPTY Yes Editor> PUSH_BACK a Editor> PRINT_FORWARD a Editor> QUIT UNIX>