Add Hash Table data structures

pull/748/head
mohanadtalat91 2022-04-23 07:58:58 +02:00
parent aeee10f2f0
commit a97e6c2249
21 changed files with 700 additions and 0 deletions

View File

@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767" name="Debug" optionalBuildProperties="" parent="cdt.managedbuild.config.gnu.mingw.exe.debug">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.exe.debug.1199833735" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.exe.debug">
<targetPlatform id="cdt.managedbuild.target.gnu.platform.mingw.exe.debug.1604784409" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.mingw.exe.debug"/>
<builder buildPath="${workspace_loc:/Chaining}/Debug" enableAutoBuild="true" id="cdt.managedbuild.tool.gnu.builder.mingw.base.274304429" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.exe.debug.389522833" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.exe.debug">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.960846919" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.508054827" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.debug.1388590551" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.debug">
<option id="gnu.cpp.compiler.mingw.exe.debug.option.optimization.level.693712227" name="Optimization Level" superClass="gnu.cpp.compiler.mingw.exe.debug.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
<option defaultValue="gnu.cpp.compiler.debugging.level.max" id="gnu.cpp.compiler.mingw.exe.debug.option.debugging.level.443822315" name="Debug Level" superClass="gnu.cpp.compiler.mingw.exe.debug.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1819790705" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.debug.212223772" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.debug">
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.mingw.exe.debug.option.optimization.level.1443433764" name="Optimization Level" superClass="gnu.c.compiler.mingw.exe.debug.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option defaultValue="gnu.c.debugging.level.max" id="gnu.c.compiler.mingw.exe.debug.option.debugging.level.1557706844" name="Debug Level" superClass="gnu.c.compiler.mingw.exe.debug.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.227459068" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.exe.debug.1436905175" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.exe.debug"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.exe.debug.1830516375" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.exe.debug">
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1061133508" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.exe.release.524961937">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.exe.release.524961937" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.exe.release.524961937" name="Release" optionalBuildProperties="" parent="cdt.managedbuild.config.gnu.mingw.exe.release">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.exe.release.524961937." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.exe.release.1706396377" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.exe.release">
<targetPlatform id="cdt.managedbuild.target.gnu.platform.mingw.exe.release.1454145615" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.mingw.exe.release"/>
<builder buildPath="${workspace_loc:/Chaining}/Release" id="cdt.managedbuild.tool.gnu.builder.mingw.base.465250003" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.exe.release.221007604" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.exe.release">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.302121890" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.1634002759" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.release.1038699625" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.release">
<option id="gnu.cpp.compiler.mingw.exe.release.option.optimization.level.1014495988" name="Optimization Level" superClass="gnu.cpp.compiler.mingw.exe.release.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
<option defaultValue="gnu.cpp.compiler.debugging.level.none" id="gnu.cpp.compiler.mingw.exe.release.option.debugging.level.1967234577" name="Debug Level" superClass="gnu.cpp.compiler.mingw.exe.release.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.509472916" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.release.360254318" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.release">
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.mingw.exe.release.option.optimization.level.1391134801" name="Optimization Level" superClass="gnu.c.compiler.mingw.exe.release.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option defaultValue="gnu.c.debugging.level.none" id="gnu.c.compiler.mingw.exe.release.option.debugging.level.195266905" name="Debug Level" superClass="gnu.c.compiler.mingw.exe.release.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.270967256" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.exe.release.1345244843" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.exe.release"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.exe.release.1428848002" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.exe.release">
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1740636770" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="src"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="Chaining.cdt.managedbuild.target.gnu.mingw.exe.1298972540" name="Executable" projectType="cdt.managedbuild.target.gnu.mingw.exe"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.exe.release.524961937;cdt.managedbuild.config.gnu.mingw.exe.release.524961937.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.release.1038699625;cdt.managedbuild.tool.gnu.cpp.compiler.input.509472916">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767;cdt.managedbuild.config.gnu.mingw.exe.debug.44139767.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.exe.debug.1388590551;cdt.managedbuild.tool.gnu.cpp.compiler.input.1819790705">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767;cdt.managedbuild.config.gnu.mingw.exe.debug.44139767.;cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.debug.212223772;cdt.managedbuild.tool.gnu.c.compiler.input.227459068">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.exe.release.524961937;cdt.managedbuild.config.gnu.mingw.exe.release.524961937.;cdt.managedbuild.tool.gnu.c.compiler.mingw.exe.release.360254318;cdt.managedbuild.tool.gnu.c.compiler.input.270967256">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Debug">
<resource resourceType="PROJECT" workspacePath="/Chaining"/>
</configuration>
<configuration configurationName="Release">
<resource resourceType="PROJECT" workspacePath="/Chaining"/>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
</cproject>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Chaining</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.debug.44139767" name="Debug">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="56072896265458215" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
</configuration>
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.release.524961937" name="Release">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.internal.language.settings.providers.GCCBuiltinSpecsDetectorMinGW" console="false" env-hash="56072896265458215" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetectorMinGW" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings MinGW" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
</configuration>
</project>

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,81 @@
#include "entity.h"
#include "LinearProbing.h"
#include <iostream>
#include <string>
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; i<length; i++){
cout << i << " " ;
if(!contacts[i])
cout << "(Empty)" ;
else if(contacts[i] == deletedEntity)
cout << "(Deleted)" ;
else
contacts[i]->print() ;
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 ;
}
}

View File

@ -0,0 +1,25 @@
#ifndef LINEARPROBING_H_
#define LINEARPROBING_H_
#include <vector>
#include "entity.h"
#include <iostream>
class LinearProbing {
private:
vector<entity*> 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_ */

View File

@ -0,0 +1,7 @@
#include "Node.h"
#include "entity.h"
Node::Node(entity data_) {
data = data_ ;
next = 0 ;
}

View File

@ -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_ */

View File

@ -0,0 +1,93 @@
#include "entity.h"
#include <iostream>
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_ ;
}

View File

@ -0,0 +1,27 @@
#ifndef PHONEENTRY_H
#define PHONEENTRY_H
#include <string>
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

View File

@ -0,0 +1,139 @@
#include "hashTable.h"
#include "Node.h"
#include <iostream>
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<length; i++){
if(!(int)contacts[i].size())
continue ;
cout << "Hash index : " << i << " :" ;
for(int j=0 ; j<(int)contacts[i].size(); j++){
contacts[i][j].print() ;
}
cout << '\n' ;
}
}
void hashTable::getentity(entity &contact){
int ind = contact.hash_upper_lower_digit(contact.getName(),65407)%length ;
for(int i=0; i<(int)contacts[ind].size(); i++){
if(contact.getName() == contacts[ind][i].getName()){
contact.setNameNumber(contacts[ind][i].getName(), contacts[ind][i].getNumber()) ;
return ;
}
}
cout << "There is no entity with given name " << contact.getName() ;
}
void hashTable::rehash(){
cout << "\nRehashing stratring now....\n" ;
vector< vector<entity> > 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() ;
}

View File

@ -0,0 +1,30 @@
#ifndef PHONEHASHTABLE_H
#define PHONEHASHTABLE_H
#include <string>
#include <vector>
#include "entity.h"
#include "Node.h"
class hashTable
{
private:
int length ;
int actual_len ;
double loadFactor ;
vector< vector<entity> > contacts ;
bool Is_removed(entity contact) ;
vector<Node*> 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

View File

@ -0,0 +1,84 @@
#include <iostream>
#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;
}