c++哈希表不用类实现怎么实现
时间: 2023-05-25 12:05:10 浏览: 103
哈希表可以通过使用数组和链表来实现,不需要使用类。具体实现方法如下:
1. 创建一个数组,数组的大小为哈希表的容量,也就是可以存储的元素个数。
2. 将每个元素通过哈希函数转化为一个整数,作为其在数组中的下标。
3. 如果数组中已经存在一个元素的下标和当前下标相同,就将其作为链表的一个节点,将新元素加入到链表的末尾。
4. 如果数组中不存在一个元素的下标和当前下标相同,则将新元素放在该位置。
5. 当需要查找一个元素时,先经过哈希函数计算出其在数组中的下标,然后查找该位置上的元素,如果有多个元素,则遍历链表查找。
6. 当需要删除元素时,首先通过哈希函数计算出其在数组中的下标,然后查找该位置上的元素,如果有多个元素,则遍历链表找到目标元素并进行删除操作。
这样就可以使用数组和链表来实现哈希表,而不需要使用类。
相关问题
c++哈希表不用类实现怎么代码实现
哈希表的实现依赖于哈希函数的设计,下面是一个简单的示例,使用数组作为哈希表,不使用类的实现方式:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 1000
typedef struct entry {
char *key;
int value;
struct entry *next;
} Entry;
Entry *table[TABLE_SIZE] = { NULL };
unsigned int hash(const char *key) {
unsigned int hash = 0;
for (int i = 0; key[i] != '\0'; i++) {
hash = hash * 31 + key[i];
}
return hash % TABLE_SIZE;
}
void put(const char *key, int value) {
Entry *new_entry = (Entry *) malloc(sizeof(Entry));
new_entry->key = strdup(key);
new_entry->value = value;
unsigned int h = hash(key);
new_entry->next = table[h];
table[h] = new_entry;
}
int get(const char *key) {
unsigned int h = hash(key);
Entry *entry = table[h];
while (entry != NULL) {
if (strcmp(entry->key, key) == 0) {
return entry->value;
}
entry = entry->next;
}
return -1;
}
int main() {
put("apple", 1);
put("banana", 2);
put("cherry", 3);
printf("%d\n", get("apple"));
printf("%d\n", get("banana"));
printf("%d\n", get("cherry"));
return 0;
}
```
这里定义了一个`Entry`结构体,表示哈希表的每个元素,其中包含键(`key`)和值(`value`)。哈希表是一个`Entry`指针数组`table`,其中每个元素是一个指向`Entry`结构体的指针。
哈希函数`hash`使用了经典的字符串哈希函数,将字符串转化为一个哈希值,然后使用取模运算得到哈希表的下标。
`put`函数向哈希表中插入一个元素,首先使用`hash`函数计算出该元素的哈希值,然后创建一个新的`Entry`结构体,将键和值赋值给它,并且将该元素插入到哈希表的链表中,链表使用`next`指针实现。
`get`函数用于查找哈希表中的元素,首先根据键值计算出哈希值作为下标,然后遍历该位置的链表,查找键是否匹配,如果找到则返回对应的值。如果没有找到则返回-1。
上述代码仅仅是一个简单的实现,实际情况中还需要考虑冲突解决、扩容、删除等问题。
c++哈希表不用类和结构体实现怎么代码实现
哈希表可以用数组和链表实现,具体实现方式如下:
1. 定义一个数组,数组的长度为哈希表中键值对的最大数量(一般为质数),数组中每个元素的初始值为 NULL。
2. 定义一个哈希函数,将键值通过该函数转换成数组下标。
3. 实现插入函数。首先使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否已经存在,如果存在,则更新其对应的值;如果不存在,则在链表头插入新节点,节点的值为键值对。
4. 实现查找函数。同样使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否存在,如果存在,则返回其对应的值;如果不存在,则返回空值。
5. 实现删除函数。同样使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否存在,如果存在,则删除该节点,否则不做任何处理。
下面是一个实现哈希表的示例代码(使用 C 语言实现):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 10007
#define KEY_SIZE 128
struct Node {
char key[KEY_SIZE];
char value[KEY_SIZE];
struct Node* next;
};
struct Node* hashtable[MAX_SIZE] = { NULL };
int hash(char* key) {
int sum = 0;
for (int i = 0; i < strlen(key); i++) {
sum += key[i];
}
return sum % MAX_SIZE;
}
void insert(char* key, char* value) {
int index = hash(key);
struct Node* p = hashtable[index];
while (p != NULL) {
if (strcmp(p->key, key) == 0) {
strcpy(p->value, value);
return;
}
p = p->next;
}
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
strcpy(node->key, key);
strcpy(node->value, value);
node->next = hashtable[index];
hashtable[index] = node;
}
char* find(char* key) {
int index = hash(key);
struct Node* p = hashtable[index];
while (p != NULL) {
if (strcmp(p->key, key) == 0) {
return p->value;
}
p = p->next;
}
return NULL;
}
void remove_key(char* key) {
int index = hash(key);
struct Node* p = hashtable[index];
if (p == NULL) {
return;
}
if (strcmp(p->key, key) == 0) {
hashtable[index] = p->next;
free(p);
return;
}
while (p->next != NULL) {
if (strcmp(p->next->key, key) == 0) {
struct Node* node = p->next;
p->next = node->next;
free(node);
return;
}
p = p->next;
}
}
int main() {
insert("hello", "world");
insert("foo", "bar");
printf("%s\n", find("hello")); // output: world
printf("%s\n", find("foo")); // output: bar
remove_key("foo");
printf("%s\n", find("foo")); // output: (null)
return 0;
}
```