- C语言数据结构与算法教程
- C语言数据结构与算法 - 首页
- C语言数据结构与算法 - 概述
- C语言数据结构与算法 - 环境配置
- C语言数据结构与算法 - 算法
- C语言数据结构与算法 - 概念
- C语言数据结构与算法 - 数组
- C语言数据结构与算法 - 链表
- C语言数据结构与算法 - 双向链表
- C语言数据结构与算法 - 循环链表
- C语言数据结构与算法 - 栈
- C语言数据结构与算法 - 表达式解析
- C语言数据结构与算法 - 队列
- C语言数据结构与算法 - 优先队列
- C语言数据结构与算法 - 树
- C语言数据结构与算法 - 散列表
- C语言数据结构与算法 - 堆
- C语言数据结构与算法 - 图
- C语言数据结构与算法 - 搜索技术
- C语言数据结构与算法 - 排序技术
- C语言数据结构与算法 - 递归
- C语言数据结构与算法 - 有用资源
- C语言数据结构与算法 - 快速指南
- C语言数据结构与算法 - 有用资源
- C语言数据结构与算法 - 讨论
C语言数据结构与算法 - 散列表
概述
散列表是一种数据结构,其插入和搜索操作速度非常快,与散列表的大小无关。其时间复杂度接近常数或O(1)。散列表使用数组作为存储介质,并使用散列技术生成元素插入或查找的索引。
散列
散列是一种将一系列键值转换为数组索引范围的技术。我们将使用模运算符来获取一系列键值。考虑一个大小为20的散列表示例,以及要存储的以下项目。项目采用(键,值)格式。
(1,20)
(2,70)
(42,80)
(4,25)
(12,44)
(14,32)
(17,11)
(13,78)
(37,98)
| 序号 | 键 | 散列值 | 数组索引 |
|---|---|---|---|
| 1 | 1 | 1 % 20 = 1 | 1 |
| 2 | 2 | 2 % 20 = 2 | 2 |
| 3 | 42 | 42 % 20 = 2 | 2 |
| 4 | 4 | 4 % 20 = 4 | 4 |
| 5 | 12 | 12 % 20 = 12 | 12 |
| 6 | 14 | 14 % 20 = 14 | 14 |
| 7 | 17 | 17 % 20 = 17 | 17 |
| 8 | 13 | 13 % 20 = 13 | 13 |
| 9 | 37 | 37 % 20 = 17 | 17 |
线性探测
我们可以看到,所使用的散列技术可能会创建已经使用的数组索引。在这种情况下,我们可以通过查找下一个空单元格来搜索数组中的下一个空位置,直到找到一个空单元格。此技术称为线性探测。
| 序号 | 键 | 散列值 | 数组索引 | 线性探测后,数组索引 |
|---|---|---|---|---|
| 1 | 1 | 1 % 20 = 1 | 1 | 1 |
| 2 | 2 | 2 % 20 = 2 | 2 | 2 |
| 3 | 42 | 42 % 20 = 2 | 2 | 3 |
| 4 | 4 | 4 % 20 = 4 | 4 | 4 |
| 5 | 12 | 12 % 20 = 12 | 12 | 12 |
| 6 | 14 | 14 % 20 = 14 | 14 | 14 |
| 7 | 17 | 17 % 20 = 17 | 17 | 17 |
| 8 | 13 | 13 % 20 = 13 | 13 | 13 |
| 9 | 37 | 37 % 20 = 17 | 17 | 18 |
基本操作
以下是散列表的基本主要操作。
搜索 - 在散列表中搜索元素。
插入 - 将元素插入到散列表中。
删除 - 从散列表中删除元素。
数据项
定义一个数据项,其中包含一些数据和键,根据该键在散列表中进行搜索。
struct DataItem {
int data;
int key;
};
散列方法
定义一个散列方法来计算数据项键的散列码。
int hashCode(int key){
return key % SIZE;
}
搜索操作
每当要搜索元素时,计算传递的键的散列码,并使用该散列码作为数组中的索引来定位元素。如果在计算出的散列码处找不到元素,则使用线性探测来获取前面的元素。
struct DataItem *search(int key){
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL){
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
插入操作
每当要插入元素时,计算传递的键的散列码,并使用该散列码作为数组中的索引来定位索引。如果在计算出的散列码处找到元素,则使用线性探测查找空位置。
void insert(int key,int data){
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] !=NULL &&
hashArray[hashIndex]->key != -1){
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
删除操作
每当要删除元素时,计算传递的键的散列码,并使用该散列码作为数组中的索引来定位索引。如果在计算出的散列码处找不到元素,则使用线性探测来获取前面的元素。找到后,在那里存储一个虚拟项以保持散列表的性能完整。
struct DataItem* delete(struct DataItem* item){
int key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL){
if(hashArray[hashIndex]->key == key){
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
示例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
struct DataItem {
int data;
int key;
};
struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key){
return key % SIZE;
}
struct DataItem *search(int key){
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL){
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void insert(int key,int data){
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] !=NULL &&
hashArray[hashIndex]->key != -1){
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item){
int key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL){
if(hashArray[hashIndex]->key == key){
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void display(){
int i=0;
for(i=0; i<SIZE; i++) {
if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
int main(){
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);
if(item != NULL){
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
delete(item);
item = search(37);
if(item != NULL){
printf("Element found: %d\n", item->data);
} else {
printf("Element not found\n");
}
}
如果我们编译并运行上面的程序,它将产生以下结果:
~~ (1,20) (2,70) (42,80) (4,25) ~~ ~~ ~~ ~~ ~~ ~~ ~~ (12,44) (13,78) (14,32) ~~ ~~ (17,11) (37,97) ~~ Element found: 97 Element not found
广告