#ifndef _HASHTABLE_H_ #define _HASHTABLE_H_ #include <iostream> #include <cstdlib> 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; intInitiate (Hashtable hash *, int size); // initialize the hash table int the Find (* Hashtable hash, DataType x); // Find the element x corresponding keyword int INSERT (* Hashtable hash, DataType x); // Like inserted into the hash table array element x, and it is provided corresponding to the keyword int deleted (the hash Hashtable *, DataType x); // delete the data element x from the hash table void the destroy (the hash Hashtable *); // undo function / * int main () { System ( "PAUSE"); return 0; } * / int Initiate (the hash Hashtable *, int size) { the hash -> ARR = (hashitem *) the malloc (the sizeof (hashitem) * size); // initialize the array hash-> table_size = size; IF (hash-> ARR == NULL) { COUT << " Initialization Failed " << endl; return 0 ; } the else { the hash -> current_size = 0 ; return . 1 ; } } int Find (* Hashtable the hash, DataType x) // find keywords corresponding element x { int I = x.key% hash-> table_size; int= J I; the while (! hash-> ARR [J] == .info the Active && hash-> ARR [J] = .data.key x.key) { J = (J + . 1 ) & hash-> table_size; // with The method continues to find hash collision IF (J == I) { COUT << " traverse this hash table is not found " << endl; return -hash-> table_size; } } IF (hash-> ARR [J]. == info the Active) { return J; } the else { return - J; } } int insert(hashtable* hash, datatype x) { int i = find(hash, x); if (i > 0) { cout << "该数据元素已经存在了!" << 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 << "没有这个元素,无法删除!" << endl; return 0; } } void destroy(hashtable* hash) { delete[]hash->arr; } #endif