C++实现的简单hashtable(哈希表)范例的代码

下面的代码段是关于C++实现的简单hashtable(哈希表)范例的代码。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#define SIZE_KEY 16
#define SIZE_VALUE1 64
#define SIZE_VALUE2 16
#define DEFAULT_TABLESIZE 101
using namespace std;

struct NODE
{
{
strcpy(Key, Key1);
strcpy(FullName, fName);
strcpy(Tele_No, tele);
Salary = sal;
next = NULL;
}
char Key[SIZE_KEY];
char FullName[SIZE_VALUE1];
char Tele_No[SIZE_VALUE2];
double Salary;
double Tax;
};

class Hashtable
{
private:
int table_size;
int size;
int current_index;
public:
void removeAll();
int getSize();
void initIterator();
bool hasNext();
};

Hashtable::Hashtable(int T)
{
size = 0;
table_size = T;
for(int i=0; i<table_size; i++)
{
table[i] = NULL;
}
}

Hashtable::~Hashtable()
{
removeAll();
delete[] table;
}

{
cout << "nKey: " << N1->Key << "nFullName: "
<< N1->FullName <<"nTele.: " << N1->Tele_No << "nSalary: "
<< setprecision(12) << N1->Salary<< "nTax: " << N1->Tax << endl;
}

if(find(N->Key) != NULL)
{
return false;
}
int bucket = hashString(N->Key);
entry->next = table[bucket];
table[bucket] = entry;
size++;
return true;


if(temp == NULL)
{
N->FullName[0] = '0';
return false;
}
else
{
strcpy(N->FullName, temp->FullName);
strcpy(N->Tele_No, temp->Tele_No);
N->Salary = temp->Salary;
N->Tax = temp->Tax;
return true;
}

if(find(Key) == NULL)
{
return false;
}
else
{
return true;
}


int bucket = hashString(Key);
if(temp == NULL)
{
return false;
}
else if(strcmp(Key, temp->Key) == 0)
{
table[bucket] = temp->next;
delete temp;
size--;
return true;
}
else
{
while(temp_next != NULL)
{
if(strcmp(Key, temp_next->Key) == 0)
{
temp->next = temp_next->next;
delete temp_next;
size--;
return true;
}
temp = temp->next;
temp_next = temp_next->next;
}
}
return false;


void Hashtable::removeAll()
for(int i=0; i<table_size; i++)
{
while(temp != NULL)
{
disp(temp);
delete temp;
temp = next;
}
}
size = 0;

int Hashtable::getSize()
{
return size;
}

int bucket = hashString(Key);
while(temp != NULL)
{
if(strcmp(Key, temp->Key) == 0)
{
return temp;
}
temp = temp->next;
}
return NULL;

int n = strlen(Key);
long h = 0;
for(int i=0; i<n; i++)
{
h = (h << 3) ^ Key[i];
}
return abs(h % table_size );

void Hashtable::initIterator()
current_entry = NULL;
current_index = table_size;
for(int i=0; i<table_size; i++)
{
if(table[i] == NULL)
{
continue;
}
else
{
current_entry = table[i];
current_index = i;
break;
}
}

bool Hashtable::hasNext()
{
if(current_entry == NULL)
{
return false;
}
else
{
return true;
}
}
{
if(current_entry == NULL)
{
Key[0] = '0';
return;
}
strcpy(Key, current_entry->Key);
if(current_entry->next != NULL)
{
current_entry = current_entry->next;
}
else
{
for(int i=current_index+1; i<table_size; i++)
{
if(table[i] == NULL)
{
continue;
}
current_entry = table[i];
current_index = i;
return;
}
current_entry = NULL;
current_index = table_size;
}
}


int main()
{
char temp1[SIZE_KEY];

NODE N1("389","Mariam","8216734",22123.267);

if(!hashtable->contains(N1.Key))
{
cout << "nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}

strcpy(N1.Key, "314");
strcpy(N1.FullName, "Zeki");
strcpy(N1.Tele_No, "8765623");
N1.Salary = 98124.567;
if(!hashtable->contains(N1.Key))
{
cout << "nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}

strcpy(N1.Key, "320");
strcpy(N1.FullName, "Murad");
strcpy(N1.Tele_No, "7231144");
N1.Salary = 19834.575;
if(!hashtable->contains(N1.Key))
{
cout << "nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}

strcpy(N1.Key, "768");
strcpy(N1.FullName, "Hassan");
strcpy(N1.Tele_No, "7689876");
N1.Salary = 45124.755;
if(!hashtable->contains(N1.Key))
{
cout << "nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}

strcpy(N1.Key, "756");
strcpy(N1.FullName, "Ali");
strcpy(N1.Tele_No, "9874545");
N1.Salary = 43554.125;
if(!hashtable->contains(N1.Key))
{
cout << "nAdding node: ";
disp(&N1);
hashtable->put(&N1);
}

dispAll(hashtable);

strcpy(temp1,"314");
hashtable->remove(temp1);
cout << "nnAfter removing 314:" << endl;
dispAll(hashtable);
cout << "nnDestroying hashtable:" << endl;
delete hashtable;
return 0;
}

{
NODE N1;
cout << "nnCurrent nodes in hashtable:" << endl;
hashtable->initIterator();
while(hashtable->hasNext())
{
hashtable->getNextKey(N1.Key);
hashtable->get(&N1);
disp(&N1);
}
}

Program's output
Adding node:
Key: 389
FullName: Mariam
Tele.: 8216734
Salary: 22123.267
Tax: 110.616335

Adding node:
Key: 314
FullName: Zeki
Tele.: 8765623
Salary: 98124.567
Tax: 110.616335

Adding node:
Key: 320
FullName: Murad
Tele.: 7231144
Salary: 19834.575
Tax: 110.616335

Adding node:
Key: 768
FullName: Hassan
Tele.: 7689876
Salary: 45124.755
Tax: 110.616335

Adding node:
Key: 756
FullName: Ali
Tele.: 9874545
Salary: 43554.125
Tax: 110.616335


Current nodes in hashtable:

Key: 768
FullName: Hassan
Tele.: 7689876
Salary: 45124.755
Tax: 225.623775

Key: 314
FullName: Zeki
Tele.: 8765623
Salary: 98124.567
Tax: 490.622835

Key: 756
FullName: Ali
Tele.: 9874545
Salary: 43554.125
Tax: 217.770625

Key: 389
FullName: Mariam
Tele.: 8216734
Salary: 22123.267
Tax: 110.616335

Key: 320
FullName: Murad
Tele.: 7231144
Salary: 19834.575
Tax: 99.172875


After removing 314:


Current nodes in hashtable:

Key: 768
FullName: Hassan
Tele.: 7689876
Salary: 45124.755
Tax: 225.623775

Key: 756
FullName: Ali
Tele.: 9874545
Salary: 43554.125
Tax: 217.770625

Key: 389
FullName: Mariam
Tele.: 8216734
Salary: 22123.267
Tax: 110.616335

Key: 320
FullName: Murad
Tele.: 7231144
Salary: 19834.575
Tax: 99.172875


Destroying hashtable:

Key: 768
FullName: Hassan
Tele.: 7689876
Salary: 45124.755
Tax: 225.623775

Key: 756
FullName: Ali
Tele.: 9874545
Salary: 43554.125
Tax: 217.770625

Key: 389
FullName: Mariam
Tele.: 8216734
Salary: 22123.267
Tax: 110.616335

Key: 320
FullName: Murad
Tele.: 7231144
Salary: 19834.575
Tax: 99.172875

Process returned 0 (0x0) execution time : 0.162 s
Press any key to continue.





猜你喜欢

转载自www.cnblogs.com/mantiger/p/10338695.html