开放定址散列表(平方探测法)实现文件C语言
2010-10-21 12:55
423 查看
/* open_addressing_hash(2) -- 开放定址散列表实现文件 */ #include <stdio.h> #include <stdlib.h> #include "open_addressing_hash(2).h" /* 局部函数声明 */ static int Get_Prime_Value (const int size) ; static int Is_An_Prime (const int number) ; static int Square_ (const int i) ; /* 接口函数定义 */ int Hash (const int item, const int size) { return item % size ; } int InitializeTable (HashTable * const ph, const int size) { int temp, count ; (*ph) = (struct hashtable *) malloc (sizeof (struct hashtable)) ; if (NULL == *ph) { puts ("Out of space[1].") ; return 0 ; } temp = (*ph) -> size = Get_Prime_Value (size) ; (*ph) -> lists = (Cell *) malloc (sizeof (Cell) * temp) ; if (NULL == (*ph) -> lists) { free (*ph) ; puts ("Out of space.[2]") ; return 0 ; } for (count = 0; count < temp; count++) (*ph) -> lists[count].entry = Empty ; return 1 ; } Cell * Find (const HashTable * const ph, const Item item) { int key, size, critical, value, index, i = 0 ; size = (*ph) -> size ; key = Hash (item, size) ; critical = size / 2 + 1 ; do { if (i <= critical) { value = Square_ (i++) ; index = (key + value) % size ; } else break ; } while (Legitimate == (*ph) -> lists[index].entry && (*ph) -> lists[index].item != item) ; return (*ph) -> lists + index ; } int Insert (const HashTable * const ph, const Item item) { Cell * position ; position = Find (ph, item) ; if (Empty == position -> entry || Deleted == position -> entry) { position -> entry = Legitimate ; position -> item = item ; return 1 ; } return 0 ; } int Delete (const HashTable * const ph, const Item item) { Cell * position ; position = Find (ph, item) ; if (Legitimate == position -> entry && item == position -> item) { position -> entry = Deleted ; return 1 ; } return 0 ; } void Traversal (const HashTable * const ph, void (* pfun) (const Cell cell)) { int count, size = (*ph) -> size ; for (count = 0; count < size; count++) (* pfun) ((*ph) -> lists[count]) ; } void Release (const HashTable * const ph) { free ((*ph) -> lists) ; free (*ph) ; } /* 局部函数定义 */ static int Get_Prime_Value (const int size) { int real_size = size ; while (!Is_An_Prime (real_size)) real_size++ ; return real_size ; } static int Is_An_Prime (const int number) { int count, value ; if (number < 2) { puts ("Wrong input.") ; return 0 ; } for (count = 2, value = number / 2 + 1; count < value; count++) if (0 == number % count) return 0 ; return 1 ; } static int Square_ (const int i) { return i * i ; }
相关文章推荐
- 开放定址散列表(线性探测法(双散列))实现文件C语言
- 再散列开放定址散列表(线性探测法)加强版可再散列经优化实现文件C语言
- 开放定址散列表(线性探测法)实现文件C语言
- 开放定址散列表(线性探测法(双散列))头文件C语言
- 开放定址散列表(线性探测法)头文件C语言
- 开放定址散列表(平方探测法)头文件C语言
- 开放定址散列表--C语言实现
- 数据结构与算法分析-开放定址散列表的实现
- 再散列--开放定址散列表(线性探测法)加强版可再散列经优化头文件C语言
- 数据结构与算法分析-开放定址散列表的实现
- c语言实现最简单的哈希表(开放地址线性探测法)
- 散列的平方探测法实现
- 开放定址平方探测散列表
- 数据结构 c语言实现哈希(hash)表查找 除留余数法构建hash函数开放定值法线性探测处理冲突
- 通讯录c语言实现(动态内存开辟&文件存储)
- C语言实现文件复制
- <5>c语言实现ATM存取款系统(文件、排序)
- linux下用c语言实现ls -l 对文件或者目录的属性遍历
- 栈数组实现头文件C语言
- linux下c语言实现多线程文件复制