diff --git a/algorithms/CPlusPlus/Hash-Table/.cproject b/algorithms/CPlusPlus/Hash-Table/.cproject new file mode 100644 index 00000000..04a292f8 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/.cproject @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/CPlusPlus/Hash-Table/.project b/algorithms/CPlusPlus/Hash-Table/.project new file mode 100644 index 00000000..6713b110 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/.project @@ -0,0 +1,26 @@ + + + Chaining + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/algorithms/CPlusPlus/Hash-Table/.settings/language.settings.xml b/algorithms/CPlusPlus/Hash-Table/.settings/language.settings.xml new file mode 100644 index 00000000..c8cb37cc --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/algorithms/CPlusPlus/Hash-Table/.settings/org.eclipse.cdt.managedbuilder.core.prefs b/algorithms/CPlusPlus/Hash-Table/.settings/org.eclipse.cdt.managedbuilder.core.prefs new file mode 100644 index 00000000..28448e8c --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/.settings/org.eclipse.cdt.managedbuilder.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/CPATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/CPATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/CPLUS_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/CPLUS_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/C_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/C_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/append=true +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/appendContributed=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/LIBRARY_PATH/delimiter=; +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/LIBRARY_PATH/operation=remove +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/append=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.44139767/appendContributed=true diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/Chaining.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/Chaining.o new file mode 100644 index 00000000..13313a8c Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/Chaining.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/LinearProbing.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/LinearProbing.o new file mode 100644 index 00000000..a2f02550 Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/LinearProbing.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/Node.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/Node.o new file mode 100644 index 00000000..787c88e4 Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/Node.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/PhoneEntry.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/PhoneEntry.o new file mode 100644 index 00000000..6ef29c19 Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/PhoneEntry.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/entity.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/entity.o new file mode 100644 index 00000000..61133afd Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/entity.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/hashTable.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/hashTable.o new file mode 100644 index 00000000..3f778d96 Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/hashTable.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/Debug/src/main.o b/algorithms/CPlusPlus/Hash-Table/Debug/src/main.o new file mode 100644 index 00000000..b0c510b8 Binary files /dev/null and b/algorithms/CPlusPlus/Hash-Table/Debug/src/main.o differ diff --git a/algorithms/CPlusPlus/Hash-Table/README.md b/algorithms/CPlusPlus/Hash-Table/README.md new file mode 100644 index 00000000..9f58c470 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/README.md @@ -0,0 +1,9 @@ +# Hash Table + + + +- Implementation of Hash Table with different hashing functions, Implement two ways to solve colliosion : + + - [x] Chaining + - [x] Linear Probing + diff --git a/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.cpp b/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.cpp new file mode 100644 index 00000000..9233772c --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.cpp @@ -0,0 +1,81 @@ +#include "entity.h" +#include "LinearProbing.h" +#include +#include + +LinearProbing::LinearProbing(int length_) { + actual_length = 0 ; + length = length_ ; + contacts.resize(length_) ; + deletedEntity = new entity("",-1) ; +} + +void LinearProbing::add(entity contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + for(int i=0; i<(int)contacts.size(); i++){ + if(contacts[ind] == deletedEntity || !contacts[ind]){ + contacts[ind] = new entity(contact.getName(),contact.getNumber()) ; + return; + } + else if(contact.getName() == contacts[ind]->getName()){ + contacts[ind]->setNameNumber(contact.getName(),contact.getNumber()) ; + actual_length++ ; + return; + } + ind = (ind+1)%length ; + } + cout << "Can't add it, cause there is no an empty cell. \n" ; + +} + +void LinearProbing::remove(entity contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + for(int i=0; i<(int)contacts.size(); i++){ + if(!contacts[ind]){ + cout << "There is no item called (" << contact.getName() << ")" << '\n'; + return; + } + else if(contacts[ind]->getName() == contact.getName()){ + delete contacts[ind] ; + contacts[ind] = deletedEntity ; + actual_length-- ; + return; + } + ind = (ind+1)%length ; + } + cout << "There is no item called " << contact.getName() << ")"; + +} + +void LinearProbing::printall(){ + cout << "Hash Table :- " << endl ; + for(int i=0; iprint() ; + cout << '\n' ; + } +} + +void LinearProbing::getContact(entity &contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + for(int i=0; i<(int)contacts.size(); i++){ + if(!contacts[ind]){ + cout << "There is no item called (" << contact.getName() << ")" << '\n'; + return; + } + else if(contacts[ind]->getName() == contact.getName()){ + contact = *contacts[ind] ; + return; + } + ind = (ind+1)%length ; + } +} + + + + diff --git a/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.h b/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.h new file mode 100644 index 00000000..bebb6445 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/LinearProbing.h @@ -0,0 +1,25 @@ + +#ifndef LINEARPROBING_H_ +#define LINEARPROBING_H_ + +#include +#include "entity.h" +#include + +class LinearProbing { +private: + vector contacts ; + int length ; + int actual_length ; + entity* deletedEntity ; + +public: + LinearProbing(int length); + void add(entity contact) ; + void remove(entity contact) ; + void printall() ; + void getContact(entity &contact) ; + +}; + +#endif /* LINEARPROBING_H_ */ diff --git a/algorithms/CPlusPlus/Hash-Table/src/Node.cpp b/algorithms/CPlusPlus/Hash-Table/src/Node.cpp new file mode 100644 index 00000000..9c354204 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/Node.cpp @@ -0,0 +1,7 @@ +#include "Node.h" +#include "entity.h" +Node::Node(entity data_) { + data = data_ ; + next = 0 ; +} + diff --git a/algorithms/CPlusPlus/Hash-Table/src/Node.h b/algorithms/CPlusPlus/Hash-Table/src/Node.h new file mode 100644 index 00000000..c3dac183 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/Node.h @@ -0,0 +1,16 @@ +#include "entity.h" + +#ifndef NODE_H_ +#define NODE_H_ + +class Node { + + +public: + + entity data ; + Node* next ; + Node(entity data_) ; +}; + +#endif /* NODE_H_ */ diff --git a/algorithms/CPlusPlus/Hash-Table/src/entity.cpp b/algorithms/CPlusPlus/Hash-Table/src/entity.cpp new file mode 100644 index 00000000..7bf140a8 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/entity.cpp @@ -0,0 +1,93 @@ +#include "entity.h" + +#include + +using namespace std ; + +entity::entity(){ + name = "" ; + phoneNumber = -1 ; +} + +entity::entity(string name_, int phoneNumber_){ + name = name_ ; + phoneNumber = phoneNumber_ ; +} + +int entity::hash_lower(){ + long long nn = INTERNAL_LIMIT ; + long long sum = 0 ; + for(int i=0; i < (int)name.size(); i++){ + sum = (sum*26 + name[i]-'a') % nn ; + } + return sum % nn ; +} + +int entity::hash_upper_lower_digit(string str, int num){ + long long nn = INTERNAL_LIMIT ; + long long sum = 0, temp = 0 ; + int base = 26 + 26 + 10 ; // The first 26 for upperLetter, the second 26 for lowerLetter, the last 10 for digits(0:9) + + for(int i=0; i<(int)name.size(); i++){ + if(name[i] >= 'a' && name[i] <= 'z'){ + temp = name[i]-'a' ; + } + else if(name[i] >= 'A' && name[i] <= 'Z'){ + temp = 26 + name[i]-'A' ; + } + else if(name[i] >= '0' && name[i] <= '9'){ + temp = 26+26 + name[i]-'0' ; + } + sum = (base*sum+temp) % nn ; + } + + return sum ; + +} + +int entity::folding_hash(string str){ + long long sum = 0, n_ = INTERNAL_LIMIT ; + for(int i=0; i<(int)name.size(); i+=4){ + string ans = " " ; + for(int j=0; j<4; j++){ + ans += name[j] ; + } + + sum += hash_upper_lower_digit(ans,phoneNumber) ; + sum %= n_ ; + } + return sum ; +} + +int entity::hash_num(int num){ + long long n_ = INTERNAL_LIMIT ; + return (num % n_ + n_) % n_ ; +} + +int entity::hash_multiple(string str, int num){ + long long n_ = INTERNAL_LIMIT, sum = 0 ; + sum += hash_upper_lower_digit(str,num) ; + sum += hash_num(num) ; + sum %= n_ ; + + return sum ; +} + + +void entity::print(){ + cout << "(Name: " << name << " " << ",Phone Number:" << phoneNumber << ") " ; +} + +string entity::getName(){ + return name ; +} + +int entity::getNumber(){ + return phoneNumber ; +} + + +void entity::setNameNumber(string name_, int number_){ + name = name_ ; + phoneNumber = number_ ; +} diff --git a/algorithms/CPlusPlus/Hash-Table/src/entity.h b/algorithms/CPlusPlus/Hash-Table/src/entity.h new file mode 100644 index 00000000..5bf0acac --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/entity.h @@ -0,0 +1,27 @@ +#ifndef PHONEENTRY_H +#define PHONEENTRY_H +#include + +using namespace std ; + +class entity +{ +private: + const static int INTERNAL_LIMIT = 2147483647; + string name ; // we'll be hash according to name + int phoneNumber ; +public: + entity() ; + entity(string name_, int phoneNumber_) ; + string getName() ; + int getNumber() ; + void setNameNumber(string name_, int number) ; + int hash_lower() ; // use only lower case letter for hashing + int hash_upper_lower_digit(string str, int num) ; // Hash according to name only + void print() ; + int folding_hash(string str) ; + int hash_num(int num) ; + int hash_multiple(string str, int num) ; // To hash according to name & phoneNumber +}; + +#endif // PHONEENTRY_H diff --git a/algorithms/CPlusPlus/Hash-Table/src/hashTable.cpp b/algorithms/CPlusPlus/Hash-Table/src/hashTable.cpp new file mode 100644 index 00000000..44754ae8 --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/hashTable.cpp @@ -0,0 +1,139 @@ +#include "hashTable.h" + +#include "Node.h" +#include + +using namespace std ; + +hashTable::hashTable(int length_, double limitLoadFactor){ + length = length_ ; + loadFactor = limitLoadFactor ; + contacts.resize(length_) ; + contactsOfLinkedlist.resize(length_) ; + actual_len = 0 ; +} + +void hashTable::add(entity contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + + for(int i=0 ; i<(int)contacts[ind].size() ; i++){ + if(contacts[ind][i].getName() == contact.getName()){ + contacts[ind][i] = contact ; + return; + } + } + actual_len++ ; + double ratio = (double)actual_len/length ; + cout << "Current load factor : " << ratio << '\n' ; + if(ratio >= loadFactor){ + rehash() ; + } + contacts[ind].push_back(contact) ; +} + +void hashTable::addLinkedlist(entity contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + Node* curr = contactsOfLinkedlist[ind] ; + Node* prev = 0 ; + + if(!curr){ + contactsOfLinkedlist[ind] = new Node(contact) ; + return; + } + while(curr){ + if(curr->data.getName() == contact.getName()){ + curr->data = contact ; + return; + } + prev = curr ; + curr = curr->next ; + } + Node *contactNode = new Node(contact) ; + prev->next = contactNode ; + contactNode->next = 0 ; +} + +void hashTable::printallLinkedlist(){ + cout << "Hash Table :- " << '\n' ; + for(int i=0; i<(int)contactsOfLinkedlist.size(); i++){ + if(!contactsOfLinkedlist[i]) + continue ; + cout << "Hash index : " << i << ": " ; + Node* curr = contactsOfLinkedlist[i] ; + while(curr){ + curr->data.print() ; + curr = curr->next ; + } + cout << '\n' ; + } +} + + +bool hashTable::Is_removed(entity contact){ + int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ; + for(int j=0; (int)j<(int)contacts[ind].size(); j++){ + if(contact.getName() == contacts[ind][j].getName()){ + swap(contacts[ind][j], contacts[ind].back()) ; + contacts[ind].pop_back() ; + return true ; + } + } + return false ; +} + +void hashTable::remove(entity contact){ + if(!Is_removed(contact)) + cout << "There is no contact called " << contact.getName() << ", Please Choose Add" << endl ; + else actual_len-- ; +} + + +void hashTable::printall(){ + cout << "Hash Table :-" << '\n' ; + for(int i=0 ; i > temp ; + temp.assign(contacts.begin(), contacts.end()) ; + contacts.clear() ; + contacts.resize(2*length); + this->length *= 2 ; + this->actual_len = 0 ; + + + for(int i=0; i<(int)temp.size(); i++){ + for(int j=0; j<(int)temp[i].size(); j++){ + entity item(temp[i][j].getName(), temp[i][j].getNumber()) ; + add(item) ; + } + } + + printall() ; +} + + diff --git a/algorithms/CPlusPlus/Hash-Table/src/hashTable.h b/algorithms/CPlusPlus/Hash-Table/src/hashTable.h new file mode 100644 index 00000000..7723d9fe --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/hashTable.h @@ -0,0 +1,30 @@ +#ifndef PHONEHASHTABLE_H +#define PHONEHASHTABLE_H +#include +#include +#include "entity.h" +#include "Node.h" + +class hashTable +{ +private: + int length ; + int actual_len ; + double loadFactor ; + vector< vector > contacts ; + bool Is_removed(entity contact) ; + vector contactsOfLinkedlist ; + +public: + hashTable(int length_, double LIMIT_LOAD_FACTOR) ; + void addLinkedlist(entity contact) ; + void printallLinkedlist() ; + void add(entity contact) ; + void remove(entity contact) ; + void getentity(entity &contact) ; + void printall() ; + void rehash() ; + +}; + +#endif // PHONEHASHTABLE_H diff --git a/algorithms/CPlusPlus/Hash-Table/src/main.cpp b/algorithms/CPlusPlus/Hash-Table/src/main.cpp new file mode 100644 index 00000000..9ba13cef --- /dev/null +++ b/algorithms/CPlusPlus/Hash-Table/src/main.cpp @@ -0,0 +1,84 @@ +#include +#include "LinearProbing.h" +#include "entity.h" +#include "hashTable.h" + +using namespace std; + +int main() { + + cout << "\nNormal Hash Table start :-\n" ; + hashTable HT(8,0.5); + HT.add(entity("FirstOne",1)) ; + HT.add(entity("FirstOen",11)) ; + HT.add(entity("SecondOne",2)) ; + HT.add(entity("ThirdOne",3)) ; + HT.add(entity("FourthOne",4)) ; + HT.add(entity("FifthOne",5)) ; + HT.printall() ; + cout << '\n' ; + + HT.remove(entity("FirstOn",1)) ; // There is no item called (FirstOn) + cout << '\n' ; + + HT.remove(entity("FirstOne",1)) ; + HT.printall() ; // FirstOne entity will be deleted + cout << '\n' ; + + entity element("FirstOen",-1) ; + HT.getentity(element) ; // There is no item called (FirstOne) + element.print() ; // (Name: FirstOne ,Phone Number:-1) + cout << '\n' ; + + entity element2("FirstOn",-1) ; + HT.getentity(element2) ; // // There is no item called (FirstOn) + element2.print() ; + cout << '\n' ; + + cout << "\nEnd Normal Hash Table\n" ; + + cout << "\nStart Hash Table using Linear Probing :- \n" ; + LinearProbing HTLinearProbing(8); + HTLinearProbing.add(entity("FirstOne",1)) ; + HTLinearProbing.add(entity("FirstOen",11)) ; + HTLinearProbing.add(entity("SecondOne",2)) ; + HTLinearProbing.add(entity("ThirdOne",3)) ; + HTLinearProbing.add(entity("FourthOne",4)) ; + HTLinearProbing.add(entity("FifthOne",5)) ; + HTLinearProbing.printall() ; + cout << '\n' ; + + HTLinearProbing.remove(entity("FirstOn",1)) ; // There is no item called (FirstOn) + cout << '\n' ; + + HTLinearProbing.remove(entity("FirstOne",1)) ; + HTLinearProbing.printall() ; // FirstOne entity will be deleted + cout << '\n' ; + + entity elementLinearProbing("FirstOne",-1) ; + HTLinearProbing.getContact(elementLinearProbing) ; // There is no item called (FirstOne) + element.print() ; // (Name: FirstOne ,Phone Number:-1) + cout << '\n' ; + + entity elementLinearProbing2("FirstOn",-1) ; + HTLinearProbing.getContact(elementLinearProbing2) ; // // There is no item called (FirstOn) + element2.print() ; + cout << '\n' ; + + cout << "\nEnd Hash Table using Linear Probing \n" ; + + + cout << "\nStart using LinkedList :- \n" ; + hashTable HTLinkedlist(8,0.75); + HTLinkedlist.addLinkedlist(entity("LLFirstOne",1)) ; + HTLinkedlist.addLinkedlist(entity("LLSecondOne",2)) ; + HTLinkedlist.addLinkedlist(entity("LLThirdOne",3)) ; + HTLinkedlist.addLinkedlist(entity("LLFourthOne",4)) ; + HTLinkedlist.addLinkedlist(entity("LLFifthOne",5)) ; + HTLinkedlist.printallLinkedlist() ; + cout << "End using LinkedList \n\n" ; // It's to print the same as the above test + + + + return 0; +}