1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include<iostream>
#include<math.h>
using namespace std;
// class node
class node{
public:
int id;
char * name;
char * address;
int age;
node * next;
int GPA;
node(int num,char * nam,char * addr,int ages,int gpa){
id = num;
name = nam;
address = addr;
age = ages;
GPA = gpa;}
};
// class Hash
class hash{
public : hash();
hash(int aa,int bb,int prime,int size);
~hash();
int hashfunc(int key);
bool insert(node *n);
bool retrieve(int id2,node *nn);
bool remove(int id3);
double getloadfactor();
int a ;int b;int p; int n;
}
// constructers
hash::hash(){
a=23;b=88,n=100,p=997;
int * *table;
for(int i=0;i<n;i++){
table [i]=NULL;}
};
hash::hash(int aa,int bb,int prime,int size){
a=aa;b=bb;p=prime;n=size;
int **table;
for( int i=0;i<n;i++){
table [i]=NULL;}
};
//destructers
hash::~hash(){
node *p;
for(int j=0;j<n;j++)
while (table[j]!=NULL){
p=table[j];
table[j]=table[j]->next;
delete p;}
delete []table[j];
};
// hashing function
int hash::hashfunc(int key){
int H=((a*key+b)%p)%n;
return H;
}
// retrieve function
bool hash::retrieve(int id2,node *nn){
int s=hashfunc(id2);
bool done=true;
if(table[s]==NULL)done=false;
else{node *pp=table[s];
while(pp!=NULL && !done){
if(pp->id==id2)
{memcpy(nn,table[s],sizeof(node));
done = true;
else pp=pp->next;
}
// insert function
bool hash::insert(node *n){
int F=hash(n->id);
if(table[F]==NULL){
table[F]=n;
done=true;}
else{
n->next=table[F];
table[F]=n;
}
return done;
}
// remove function
bool hash::remove(int id3){
node *p1,*p2;
p1=p2=table[m];
int m=hashfunc(id3);
if(table[m]==NULL)done=true;
else if(table[m]->id==id3){
node *v=table[m];
table[m]=table[m]->next;
delete v;
done =true;
}
else {
while(p2!=NULL){
if(p2->id==id3){
p1->next=p2->next;
delete p2;
done = true;
}
else{ p1=p2;
p2=p2->next;}
}
}
return done;
}
// load function
double hash::getloadfactor(){
double count=0;
for(int u=0;u<n;u++){
if(table[u]!=NULL){
node *e=table[u];
while(e!=NULL){
count++;
p=p->next;}
}
}
return count/size;
}
|