Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HashO
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //HashO:Understanding Hash Table using open addressing //insert(),find(),delete() code credit goes to www.geeksforgeeks.com.Rest of the code created by Rezaul Hoque on September 06,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; ////////////////////////////////////////////////////////////////////////////// #include <iostream> class Node{ public: int key; int value; }; class HashO{ public: int cap; int sz=0; Node** a; Node* dum; HashO(int n){ this->cap=n; a=new Node*[n]; dum=new Node; dum->key= -1; dum->value= -1; } void insert(int k,int v){ Node* temp=new Node[cap]; temp->key=k; temp->value=v; int Index=k% cap; while(a[Index] !=NULL && a[Index]->key != k && a[Index]->key != -1) { Index++; Index %= cap; } if (a[Index]==NULL || a[Index]->key == -1) sz++; a[Index]=temp; } int find(int key) { int Index=key%cap; int counter =0; while (a[Index] != NULL){ if (counter++>cap) break; if (a[Index]->key==key) return a[Index]->value; Index++; Index %= cap; } return -1; } int del(int key){ int Index=key%cap; while(a[Index] !=NULL){ if(a[Index]->key==key){ a[Index]==dum; --sz; return 1; } Index++; Index %= cap; } return 0; } }; int main() { HashO h(5); for(int i=0;i<5;i++) h.a[i]=NULL; h.insert(11,5); h.insert(12,10); h.insert (13,15); h.insert(14,20); h.insert(15,25); for(int i=0;i<5;i++) std::cout<<h.a[i]->key<<" "<<h.a[i]->value<<" \n"; if(h.find(14)) std::cout<<"Value of key : "<<h.find(14)<<" \n"; else std::cout<<"It does not exist. \n"; if(h.del(13)) std::cout<<"Value of the key is deleted successfully. \n"; else std::cout<<"It does not exist. \n"; return 0; }
run
|
edit
|
history
|
help
0
3
string iteration performance
Pointer
Graph Theory 2
stack
My Type - nicro
search_n algorithm
Shultz_Lab2.CPP
override
123