C/C++一個簡單的哈希表實現

  1 #ifndef _HASHTABLE_H_
  2 #define _HASHTABLE_H_
  3 #include <iostream>
  4 #include <cstdlib>
  5 using namespace std;
  6 
  7 typedef
  8 enum {
  9     Empty, Active, Deleted
 10 }kindofitem;
 11 
 12 typedef struct
 13 {
 14     int key;
 15 }datatype;
 16 
 17 typedef struct{
 18     datatype data;
 19     kindofitem info;
 20 }hashitem;
 21 
 22 typedef struct{
 23     hashitem* arr;
 24     int table_size;
 25     int current_size;
 26 }hashtable;
 27 
 28 int initiate(hashtable* hash, int size);//初始化哈希表
 29 int find(hashtable* hash, datatype x);//查找x元素對應的關鍵字
 30 int insert(hashtable* hash, datatype x);//像哈希表中插入數組元素x,及設置它對應的關鍵字
 31 int deleted(hashtable* hash, datatype x);//從哈希表中刪除x數據元素
 32 void destroy(hashtable* hash);//撤銷函數
 33 /*
 34 int main()
 35 {
 36 
 37 system("pause");
 38 return 0;
 39 }
 40 */
 41 int initiate(hashtable* hash, int size)
 42 {
 43     hash->arr = (hashitem*)malloc(sizeof(hashitem)*size);//初始化,該數組
 44     hash->table_size = size;
 45     if (hash->arr == NULL)
 46     {
 47         cout << "初始化失敗" << endl;
 48         return 0;
 49     }
 50     else
 51     {
 52         hash->current_size = 0;
 53         return 1;
 54     }
 55 }
 56 
 57 int find(hashtable* hash, datatype x)//查找x元素對應的關鍵字
 58 {
 59     int i = x.key%hash->table_size;
 60     int j = i;
 61     while (hash->arr[j].info == Active&&hash->arr[j].data.key != x.key)
 62     {
 63         j = (j + 1)&hash->table_size;//用哈希衝突方法繼續查找
 64         if (j == i)
 65         {
 66             cout << "遍歷此哈希表,沒有找到" << endl;
 67             return -hash->table_size;
 68         }
 69     }
 70     if (hash->arr[j].info == Active)
 71     {
 72         return j;
 73     }
 74     else{
 75         return -j;
 76     }
 77 }
 78 
 79 int insert(hashtable* hash, datatype x)
 80 {
 81     int i = find(hash, x);
 82     if (i > 0)
 83     {
 84         cout << "該數據元素已經存在了!" << endl;
 85         return 0;
 86     }
 87 
 88     else if (i != -hash->table_size)
 89     {
 90         hash->arr[-i].data = x;
 91         hash->arr[-i].info = Active;
 92         hash->current_size++;
 93         return 1;
 94     }
 95     else{
 96         return 0;
 97     }
 98 }
 99 
100 int deleted(hashtable* hash, datatype x)
101 {
102     int i = find(hash, x);
103     if (i > 0)
104     {
105         hash->arr[i].info = Deleted;
106         hash->current_size--;
107         return 1;
108     }
109     else{
110         cout << "沒有這個元素,沒法刪除!" << endl;
111         return 0;
112     }
113 }
114 
115 void destroy(hashtable* hash)
116 {
117     delete[]hash->arr;
118 }
119 #endif
hashtable.h
 1 #include <iostream>
 2 using namespace std;
 3 #include "hashtable.h"
 4 void main()
 5 {
 6     hashtable myhashtable;
 7     datatype a[] = { 180, 750, 600, 430, 541, 900, 460 }, item = { 430 };
 8     int n = 7, m = 13;
 9     initiate(&myhashtable, m);
10     for (int i = 0; i < n; i++)
11     {
12         insert(&myhashtable, a[i]);
13     }
14     for (int i = 0; i < n; i++)
15     {
16         int j = find(&myhashtable, a[i]);
17         cout << "j = " << j << " " << "arr[] = " << myhashtable.arr[j].data.key << endl;
18     }
19     int k = find(&myhashtable, item);
20     if (k>0){
21         cout << "查找成功,元素" << item.key << "的哈希地址是:" << k << endl;
22     }
23     else{
24         cout << "查找失敗" << endl;
25     }
26     deleted(&myhashtable, item);
27     k = find(&myhashtable, item);
28     if (k >= 0){
29         cout << "刪除失敗!" << endl;
30     }
31     else
32     {
33         cout << "刪除成功!" << endl;
34     }
35     destroy(&myhashtable);
36     system("pause");
37     return ;
38 }
main.cpp

 設計說明:(1)哈希表的長度m不一樣,所以存放哈希表的數組採用動態數組最爲方便。初始化函數的參數msize即爲哈希表的長度。(2)哈希表的操做主要有查找,插入,刪除。其中,插入和刪除都要查找數據元素是否在哈希表中存在。查找函數一共有三種狀況:查找到,返回數據元素的哈希地址(正);未查找到,返回一個負值(插入操做可在哈希表的改返回值的絕對值位置,插入數據元素);未查找到,且哈希表已滿沒法繼續插入(此時返回值爲-tablesize)ios

(3)插入函數首先調用查找函數,當返回值(i)爲負(說明數據元素不存在)且返回值不等於-tablesize(說明哈希表未滿時候),在哈希表的-i位置插入數據元素。數組

相關文章
相關標籤/搜索