C ++ hash table file

#ifndef _HASHTABLE_H_

#define _HASHTABLE_H_
#include

#include

using namespace std;

typedef
enum {
Empty, Active, Deleted
}kindofitem;

typedef
struct
{
int key;
}datatype;

typedef
struct{
datatype data;
kindofitem info;
}hashitem;

typedef
struct{
hashitem
* arr;
int table_size;
int current_size;
}hashtable;

int initiate(hashtable* hash, int size) ;//Initialize the hash table
int find(hashtable* hash, datatype x);//< span style="color: #008000;">Find the keyword corresponding to the x element
int insert(hashtable* hash, datatype x);//< span style="color: #008000;">like inserting an array element x into a hash table and setting its corresponding keywords
int deleted(hashtable* hash, datatype x);//< span style="color: #008000;">remove the x data element from the hash table
void destroy(hashtable* hash);//Revocation function
/*

int main()
{

system("pause");
return 0;
}
*/
int initiate(hashtable* hash, int size)
{
hash
->arr = (hashitem*)malloc(sizeof(hashitem)*size);//Initialize the array
hash->table_size = size;
if (hash->arr == NULL)
{
cout
<< "Initialization failed" << endl;
return 0;
}
else
{
hash
->current_size = 0;
return 1;
}
}

int find(hashtable* hash, datatype x)//Find the keyword corresponding to the x element
{
int i = x.key%hash->table_size;
int j = i;
while (hash->arr[j].info == Active&&hash->arr[j].data.key != x.key)
{
j
= (j + 1)&hash->table_size;// Use hash collision method to continue searching
if (j == i)
{
cout
<< "traverse this hash table, but nothing is found span>" << endl;
return -hash->table_size;
}
}
if (hash->arr[j].info == Active )
{
return j;
}
else{
return -j;
}
}

int insert(hashtable* hash, datatype x)
{
int i = find(hash, x);
if (i> 0)
{
cout
<< "The data element already exists!" << endl;
return 0;
}

else if (i != -hash-> table_size)
{
hash
->arr[-i].data = x;
hash
->arr[-i].info = Active;
hash
->current_size++;
return 1;
}
else{
return 0;
}
}

int deleted(hashtable* hash, datatype x)
{
int i = find(hash, x);
if (i> 0)
{
hash
->arr[i].info = Deleted;
hash
->current_size--;
return 1;
}
else{
cout
<< "Without this element, it cannot be deleted!" << endl;
return 0;
}
}

void destroy(hashtable* hash)
{
delete[]hash->arr;
}
#endif

#ifndef _HASHTABLE_H_

#define _HASHTABLE_H_
#include

#include

using namespace std;

typedef
enum {
Empty, Active, Deleted
}kindofitem;

typedef
struct
{
int key;
}datatype;

typedef
struct{
datatype data;
kindofitem info;
}hashitem;

typedef
struct{
hashitem
* arr;
int table_size;
int current_size;
}hashtable;

int initiate(hashtable* hash, int size) ;//Initialize the hash table
int find(hashtable* hash, datatype x);//< span style="color: #008000;">Find the keyword corresponding to the x element
int insert(hashtable* hash, datatype x);//< span style="color: #008000;">like inserting an array element x into a hash table and setting its corresponding keywords
int deleted(hashtable* hash, datatype x);//< span style="color: #008000;">remove the x data element from the hash table
void destroy(hashtable* hash);//Revocation function
/*

int main()
{

system("pause");
return 0;
}
*/
int initiate(hashtable* hash, int size)
{
hash
->arr = (hashitem*)malloc(sizeof(hashitem)*size);//Initialize the array
hash->table_size = size;
if (hash->arr == NULL)
{
cout
<< "Initialization failed" << endl;
return 0;
}
else
{
hash
->current_size = 0;
return 1;
}
}

int find(hashtable* hash, datatype x)//Find the keyword corresponding to the x element
{
int i = x.key%hash->table_size;
int j = i;
while (hash->arr[j].info == Active&&hash->arr[j].data.key != x.key)
{
j
= (j + 1)&hash->table_size;// Use hash collision method to continue searching
if (j == i)
{
cout
<< "traverse this hash table, but nothing is found span>" << endl;
return -hash->table_size;
}
}
if (hash->arr[j].info == Active )
{
return j;
}
else{
return -j;
}
}

int insert(hashtable* hash, datatype x)
{
int i = find(hash, x);
if (i> 0)
{
cout
<< "The data element already exists!" << endl;
return 0;
}

else if (i != -hash-> table_size)
{
hash
->arr[-i].data = x;
hash
->arr[-i].info = Active;
hash
->current_size++;
return 1;
}
else{
return 0;
}
}

int deleted(hashtable* hash, datatype x)
{
int i = find(hash, x);
if (i> 0)
{
hash
->arr[i].info = Deleted;
hash
->current_size--;
return 1;
}
else{
cout
<< "Without this element, it cannot be deleted!" << endl;
return 0;
}
}

void destroy(hashtable* hash)
{
delete[]hash->arr;
}
#endif

Leave a Comment

Your email address will not be published.