Sir when we use operator overloading to insert dosa, in the output, dosa is not inserted at any node but its price is output?? How is this possible?
Dosa not inserted!
kindly share link of your code
I am coding in vscode, so I will just share the program, is it fine?
this is the header file that i created, just as in the video
#include #include using namespace std; //Implement a hashmap using separate chaining template class node { public: string key; T value; node *next; node(string key, T v) { this->key = key; value = v; next = NULL; } ~node() { if (next != NULL) { delete next; } } }; template class hashtable { node **table; int curr_size; int table_size; int hashfn(string key) { int idx = 0; int p = 1; for (int j = 0; j < key.length(); j++) { idx += (key[j] * p) % table_size; idx = idx % table_size; p = (p * 27) % table_size; } return idx; } void rehash() { node **oldtable = table; int oldtablesize = table_size; table_size = 2 * table_size; table = new node *[table_size]; for (int i = 0; i < table_size; i++) { table[i] = NULL; } curr_size = 0; for (int i = 0; i < oldtablesize; i++) { node temp = oldtable[i]; //copying buckets from one linked list to table(new table) while (temp != NULL) { insert(temp->key, temp->value); temp = temp->next; } if (oldtable[i] != NULL) { delete oldtable[i]; //delete keyword invokes the destructor of node class, and the destructor works recursively //to delete all nodes in the linked list until next is NULL. } } delete[] oldtable; //clears out the array of node from the old table from memory. } public: hashtable(int ts = 7) { table_size = ts; table = new node *[table_size]; curr_size = 0; for (int i = 0; i < table_size; i++) { table[i] = NULL; } } void insert(string key, T value) { int idx = hashfn(key); node n = new node(key, value); //insertion at head of linked list with index = idx. n->next = table[idx]; table[idx] = n; curr_size++; //concept of rehash.(its implementation) float loadfactor = curr_size / (1.0 * table_size); if (loadfactor > 0.7) { rehash(); } } void print() //this fn is not part of the stl hashmap class, but we are doing this for the purpose of learning. { for (int i = 0; i < table_size; i++) { node temp = table[i]; cout << "Bucket " << i << "-> "; while (temp != NULL) { cout << temp->key << “->”; temp = temp->next; } cout << endl; } } //here return type is taken as T because if the item is not present, we //cannot return -1, but we can actually return NULL. So to serve both //puposes, return type is taken as T. T *search(string key) { int idx = hashfn(key); node *temp = table[idx]; while (temp != NULL) { if (temp->key == key) { return &temp->value; } } return NULL; } T& operator[](string key) { T *f = search(key); if (f == NULL) { T garbage; insert(key, garbage); f = search(key); } return *f; } /void erase(string key) { //need to do deletion like in linked list. DO IT YOURSELF!! }/ };
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.