c语言哈希表就基本操作
时间: 2023-05-16 22:03:17 浏览: 96
哈希表(Hash Table)是一种又称为散列表的数据结构,它的主要目的是用于快速查找,它能够通过关键字快速确定唯一的数据位置,从而实现常数时间复杂度的查找。在C语言中,哈希表也拥有基本操作,下面是我对这些操作的简介:
1. 初始化哈希表:初始化哈希表需要确定哈希表的大小(通常是质数),并申请一块连续的内存空间用来存储哈希表的数据。同时,需要设置哈希表的散列函数。
2. 插入数据:首先,需要通过散列函数计算出数据所应该存储的位置,然后将数据存入哈希表中。如果该位置已经被占用,则需要进行解决冲突的处理,通常使用的是开放地址法或链地址法。
3. 查找数据:同样需要通过散列函数计算出数据所存储的位置,然后在哈希表中查找是否存在该数据。如果该位置为空,表示数据不存在;如果不为空,需要比较关键字是否与要查找的一致。
4. 删除数据:与插入数据类似,需要先计算出数据所存储的位置,然后将该位置设置为空。如果该位置处存在多个数据,同时需要将后续的数据重新散列。
总的来说,哈希表是C语言中比较常见的数据结构之一,它有着高效的查找操作,同时适用于大多数的数据类型。在实际开发中,我们可以通过调用现有库或手写代码,来完成相关的哈希表操作。
相关问题
C语言哈希表基本操作请用代码展示
好的,以下是C语言哈希表的基本操作代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 100
typedef struct Node {
char* key;
char* value;
struct Node* next;
} Node;
Node* table[TABLE_SIZE];
unsigned int hash(char* key) {
unsigned int hash = 0;
for (int i = 0; i < strlen(key); i++) {
hash = hash * 31 + key[i];
}
return hash % TABLE_SIZE;
}
void put(char* key, char* value) {
unsigned int index = hash(key);
Node* node = table[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
node->value = value;
return;
}
node = node->next;
}
node = (Node*)malloc(sizeof(Node));
node->key = key;
node->value = value;
node->next = table[index];
table[index] = node;
}
char* get(char* key) {
unsigned int index = hash(key);
Node* node = table[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
return node->value;
}
node = node->next;
}
return NULL;
}
void remove_key(char* key) {
unsigned int index = hash(key);
Node* node = table[index];
Node* prev = NULL;
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
if (prev == NULL) {
table[index] = node->next;
} else {
prev->next = node->next;
}
free(node);
return;
}
prev = node;
node = node->next;
}
}
void print_table() {
for (int i = 0; i < TABLE_SIZE; i++) {
printf("%d: ", i);
Node* node = table[i];
while (node != NULL) {
printf("(%s, %s) ", node->key, node->value);
node = node->next;
}
printf("\n");
}
}
int main() {
put("apple", "red");
put("banana", "yellow");
put("orange", "orange");
print_table();
printf("apple: %s\n", get("apple"));
printf("banana: %s\n", get("banana"));
printf("orange: %s\n", get("orange"));
remove_key("banana");
print_table();
return 0;
}
```
这段代码实现了哈希表的基本操作,包括插入、查找、删除和打印哈希表。其中,哈希函数使用了简单的字符串哈希算法,冲突解决采用了链表法。
c语言哈希表电话号码查询系统源代码
以下是一个简单的基于哈希表实现的电话号码查询系统的C语言源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 100
typedef struct node {
char name[20];
char phone[20];
struct node *next;
} Node;
Node *table[TABLE_SIZE];
unsigned int hash(char *str) {
unsigned int hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c;
return hash % TABLE_SIZE;
}
Node *find(char *name) {
unsigned int index = hash(name);
Node *p;
for (p = table[index]; p != NULL; p = p->next)
if (strcmp(name, p->name) == 0)
return p;
return NULL;
}
void insert(char *name, char *phone) {
unsigned int index = hash(name);
Node *p;
for (p = table[index]; p != NULL; p = p->next)
if (strcmp(name, p->name) == 0) {
strcpy(p->phone, phone);
return;
}
p = (Node *) malloc(sizeof(Node));
strcpy(p->name, name);
strcpy(p->phone, phone);
p->next = table[index];
table[index] = p;
}
void delete(char *name) {
unsigned int index = hash(name);
Node *p, *q;
for (q = NULL, p = table[index]; p != NULL; q = p, p = p->next)
if (strcmp(name, p->name) == 0) {
if (q == NULL)
table[index] = p->next;
else
q->next = p->next;
free(p);
return;
}
}
void display() {
int i;
Node *p;
for (i = 0; i < TABLE_SIZE; ++i)
for (p = table[i]; p != NULL; p = p->next)
printf("%s\t%s\n", p->name, p->phone);
}
int main() {
char name[20], phone[20];
int choice;
while (1) {
printf("\nTelephone directory\n");
printf("1. Find\n2. Insert\n3. Delete\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter name: ");
scanf("%s", name);
Node *p = find(name);
if (p)
printf("%s\t%s\n", p->name, p->phone);
else
printf("Name not found\n");
break;
case 2:
printf("Enter name: ");
scanf("%s", name);
printf("Enter phone number: ");
scanf("%s", phone);
insert(name, phone);
break;
case 3:
printf("Enter name: ");
scanf("%s", name);
delete(name);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
```
该程序使用了一个大小为100的哈希表来存储电话号码。它实现了四个基本操作:查找、插入、删除和显示。在运行程序时,用户可以选择要执行的操作。如果选择插入操作,则需要输入姓名和电话号码。如果选择查找操作,则需要输入姓名,程序将输出该姓名对应的电话号码。如果选择删除操作,则需要输入姓名,程序将从哈希表中删除该用户的信息。如果选择显示操作,则程序将输出哈希表中所有用户的姓名和电话号码。