Search fun in hashing

in this T can be int so we can return -1

yes you can return -1
if T is int

so why we return address T*

T is a template so it can be int

yes you can do this
then search function looks like

T search (string key){
		int idx=hashfun(key);
		Node<T>*temp=table[idx];
		while(temp){
			if(temp->key==key)return temp->value;
			temp=temp->next;
		}
		return -1;
	}

now also operator function will change and looks like

T operator [](string key){
		T f=search(key);
		if(f==-1){
			insert(key,0);
			f=search(key);
		}
		return f;
	}

so no in main you have to do some changes also

int price=h.search("Orange");
	if(price)cout<<"Price of Orange is "<<price<<endl;
	else cout<<"Not found\n";
	h.erase("Orange");
	price=h.search("Orange");
	if(price!=-1)cout<<"Price of Orange is "<<price<<endl;
	else cout<<"Not found\n";
	 cout<<h["Guvava"];

but the problem is

now you can’t update the value because you don’t access it
because you have to the address of bucket to change the value
h["Guvava"]+=32;
this is invalid statement now

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask

1 Like