#include #include #include #define HASHSIZE 1000 using namespace std; class item { public: string data; item *nxt; int tablepos; void show(); }; class list { public: void find(string finddata); void add(string in_data); void delete_current(); void display(); list(); ~list(); item *start[HASHSIZE]; item *cursor; }; void item::show() { cout << "Data: " << data << endl; cout << "Next record at: " << nxt << endl; cout << "In table position: " << tablepos << endl; } //Constructor: initialize the hash table linked lists. list::list() { for(int i = 0; i <=HASHSIZE; i++) list::start[i] = NULL; } list::~list() { item *temp, *temp2; for(int i = 0; i <= HASHSIZE; i++) { if(start[i] != NULL) { temp = start[i]; while(temp->nxt != NULL) { temp2 = temp; temp = temp2->nxt; delete temp2; } delete temp; temp = NULL; } } } void list::add(string in_data) { int hash = 0; char hashchar; item *tempitem; for (int i = 0; i < in_data.length(); i++) { //hashchar = in_data.at(i); cout << "Char: " << int(in_data.at(i)) << ", hash:"; hash += int(in_data.at(i)); cout << hash << endl; } hash = hash % HASHSIZE; //Make the hash using sum and modulus. Any good hack works here. // Now check hash location and add value to linked list there. //First check the slot. If it's empty (NULL), then this item is the first link. if (start[hash]==NULL) { start[hash] = new item; cursor = start[hash]; cursor->data = in_data; cursor->nxt = NULL; cursor->tablepos = hash; } //Otherwise, it "attaches" to a previous, existing link. else { cursor = start[hash]; while(cursor->nxt != NULL) { tempitem = cursor->nxt; cursor = tempitem; } tempitem = new item; tempitem->data = in_data; tempitem->nxt = NULL; //Create our item and populate it tempitem->tablepos = hash; cursor->nxt = tempitem; //Set the previous item 'nxt' to link to our new item. cursor = tempitem; //Set the new item as current (cursor points to current item) } } void list::delete_current() { string answer; cout << "Deleting: " << endl; cursor->show(); //Lets ask the Microsoft question here, just in case... cout << "Do you want to delete this record? (y/n)" << endl; cin >> answer; if(answer.length() == 0 || answer.at(0) != 'y') { cout << "Action cancelled." << endl; return; } //At this point, the answer starts with a 'y', so... item *temp, *temp2; int table_cursor; //Is our pointer, "cursor", valid? if(cursor == NULL) { cout << "Invalid position." << endl; return; } temp = cursor; table_cursor = cursor->tablepos; cursor = start[table_cursor]; //Is this the first link in the slot? If so, delete it and reset start[] if(temp == cursor) { start[cursor->tablepos] = NULL; delete cursor; return; } //Scroll through the items to find the item before "temp" and have "cursor" point at that. while(cursor->nxt != temp) { cursor = cursor->nxt; } // Found previous. At this point, cursor points to the "item" before temp. if(temp->nxt == NULL) { //Is "temp" the last link? If so, cursor now becomes the last. cursor->nxt = NULL; delete temp; return; } // If we get here, the record to be deleted is in the "middle" of a list. We link "cursor" // to the record past "temp" (temp->nxt) then kill temp. cursor->nxt = temp->nxt; delete temp; return; } int main(int argv, char *argc[]) { //Do some stuff. Go crazy here. list hlist; string pass = "Test"; hlist.add(pass); hlist.cursor->show(); hlist.add(pass); hlist.cursor->show(); hlist.delete_current(); }