// hash table.cpp : Defines the entry point for the console application.
// //
For More:> Hash table......, general tree..., binary tree....., Graph in c++....., General Tree.... and many more......
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct daata
{
int id;
int data;
};
struct hasher
{
daata dt[11];
int name1;
hasher();
int hash(int &id);
int rehash(int &id);
int add(daata &d);
int remove(daata &d);
void output();
};
int hasher::hash(int &id)
{
return (id%11);
}
int hasher::rehash(int &id)
{
return ((id+1)/11);
}
hasher::hasher()
{
int i;
for(i=0; i=11; i++)
{
dt[i].id=-1;
dt[i].data=0;
}
int numel=0;
}
int i=0;
int hasher::add(daata &d)
{
if(i < 11)
{
int hashed=hash(d.id);
if(hashed>=0 && hashed<=10 && dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}i++;
}
else
{
return (-1);
}
}
int hasher::remove(daata &d)
{
int hashed=hash(d.id);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}
}
void hasher::output()
{
int i;
for(i=0; i<11; i++)
{
cout<<i<<"->"<<dt[i].data<<endl;
}
}
int main(int argc, char* argv[])
{
int id=100;
int ret;
daata d;
d.data=52005;
hasher h1;
while(ret != 1)
{
d.id=id;
ret=h1.add(d);
id += (id/2);
}
d.id=271861;
h1.remove(d);
h1.output();
return 0;
}
// //
For More:> Hash table......, general tree..., binary tree....., Graph in c++....., General Tree.... and many more......
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct daata
{
int id;
int data;
};
struct hasher
{
daata dt[11];
int name1;
hasher();
int hash(int &id);
int rehash(int &id);
int add(daata &d);
int remove(daata &d);
void output();
};
int hasher::hash(int &id)
{
return (id%11);
}
int hasher::rehash(int &id)
{
return ((id+1)/11);
}
hasher::hasher()
{
int i;
for(i=0; i=11; i++)
{
dt[i].id=-1;
dt[i].data=0;
}
int numel=0;
}
int i=0;
int hasher::add(daata &d)
{
if(i < 11)
{
int hashed=hash(d.id);
if(hashed>=0 && hashed<=10 && dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}i++;
}
else
{
return (-1);
}
}
int hasher::remove(daata &d)
{
int hashed=hash(d.id);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}
}
void hasher::output()
{
int i;
for(i=0; i<11; i++)
{
cout<<i<<"->"<<dt[i].data<<endl;
}
}
int main(int argc, char* argv[])
{
int id=100;
int ret;
daata d;
d.data=52005;
hasher h1;
while(ret != 1)
{
d.id=id;
ret=h1.add(d);
id += (id/2);
}
d.id=271861;
h1.remove(d);
h1.output();
return 0;
}
useful tutorial keep it up...
EmoticonEmoticon