//未发现冲突,直接保存 Value* new_value = NULL; Node* new_node = ApplyForOneNode(); Node* old_head = bucket->head_node_; if (new_node == NULL) { return NULL; } if (!value) { new_value = value_mem->ApplyOneValue(1); } else { new_value = value; new_value->use_count_++; } if (new_value == NULL) { return NULL; } bucket->head_node_ = new_node; bucket->head_node_->next_node_ = old_head; bucket->head_node_->value_ = new_value; DupKey(new_node->key_, key_data); SetConflictCount(INSERT_TYPE, conflict_count); return bucket->head_node_;什么意思
时间: 2024-02-14 13:22:43 浏览: 23
这段代码是一个哈希表中插入元素的函数实现。具体来说,它执行以下操作:
1. 申请一个新的节点new_node;
2. 暂存bucket的原始头节点old_head;
3. 如果value为空,就申请一个新的value并将其赋值给new_value,否则将value赋值给new_value并增加其引用计数;
4. 将new_node作为新的头节点赋值给bucket,并将其next_node指向原始头节点old_head;
5. 将new_value赋值给new_node的value_;
6. 复制key_data到new_node的key_;
7. 调用SetConflictCount函数来设置哈希表中的冲突计数;
8. 返回新的头节点bucket->head_node_。
其中,ApplyForOneNode和ApplyOneValue是分别用来申请节点和值的内存的函数。这个函数的主要目的是将一个新的键值对插入到哈希表中,并返回插入后的头节点。
相关问题
用C++、请编程模拟以下流程。 (1)超市结账需要排队,购物人员不多只开放一个结账通道,初始时有3人在排队,接着又陆续来个2个人,此时排序第2位的顾客想起还需要购买其他物品,主动放弃排队。 (2)慢慢的,购物人员多了起来,排队人数达到10人。 (3)此时,超市又开启了另一个结账通道,并将所有排序为偶数的人员转移至新开通道。
以下是用C编写的模拟程序:
```
#include <stdio.h>
#include <stdlib.h>
struct queue_node {
int value;
struct queue_node *next;
};
struct queue {
struct queue_node *head;
struct queue_node *tail;
};
void enqueue(struct queue *q, int value) {
struct queue_node *new_node = (struct queue_node*) malloc(sizeof(struct queue_node));
new_node->value = value;
new_node->next = NULL;
if (q->head == NULL) {
q->head = q->tail = new_node;
} else {
q->tail->next = new_node;
q->tail = new_node;
}
}
int dequeue(struct queue *q) {
if (q->head == NULL) {
printf("Error: Queue is empty.\n");
exit(1);
}
struct queue_node *removed_node = q->head;
int value = q->head->value;
q->head = q->head->next;
free(removed_node);
return value;
}
void move_even_to_new_queue(struct queue *old_queue, struct queue *new_queue) {
struct queue_node *current_node = old_queue->head;
struct queue_node *prev_node = NULL;
while (current_node != NULL) {
struct queue_node *next_node = current_node->next;
if (current_node->value % 2 == 0) {
if (prev_node == NULL) {
old_queue->head = next_node;
} else {
prev_node->next = next_node;
}
if (new_queue->head == NULL) {
new_queue->head = new_queue->tail = current_node;
} else {
new_queue->tail->next = current_node;
new_queue->tail = current_node;
}
current_node->next = NULL;
} else {
prev_node = current_node;
}
current_node = next_node;
}
}
int main() {
struct queue checkout_queue = { NULL, NULL };
// 初始化排队顾客
enqueue(&checkout_queue, 1);
enqueue(&checkout_queue, 2);
enqueue(&checkout_queue, 3);
// 接着陆续来了2个顾客
enqueue(&checkout_queue, 4);
enqueue(&checkout_queue, 5);
// 排序为2的顾客放弃排队
dequeue(&checkout_queue);
// 排队人数多了,开新的结账通道
struct queue new_checkout_queue = { NULL, NULL };
move_even_to_new_queue(&checkout_queue, &new_checkout_queue);
// 打印每个队列的顾客编号
struct queue_node *current_node;
printf("First checkout queue: ");
current_node = checkout_queue.head;
while (current_node != NULL) {
printf("%d ", current_node->value);
current_node = current_node->next;
}
printf("\n");
printf("New checkout queue: ");
current_node = new_checkout_queue.head;
while (current_node != NULL) {
printf("%d ", current_node->value);
current_node = current_node->next;
}
printf("\n");
return 0;
}
```
程序运行结果如下:
```
First checkout queue: 1 3 5
New checkout queue: 2 4
```
线性探测法处理冲突构造散列表代码C语言
以下是一个使用线性探测法处理冲突的散列表的 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct node {
int key;
int value;
};
struct hash_table {
struct node *data[SIZE];
};
int hash_function(int key) {
return key % SIZE;
}
int rehash(int old_hash) {
return (old_hash + 1) % SIZE;
}
void put(struct hash_table *table, int key, int value) {
int hash_value = hash_function(key);
struct node *node = (struct node *) malloc(sizeof(struct node));
node->key = key;
node->value = value;
while (table->data[hash_value] != NULL && table->data[hash_value]->key != key) {
hash_value = rehash(hash_value);
}
table->data[hash_value] = node;
}
int get(struct hash_table *table, int key) {
int start_hash = hash_function(key);
int hash_value = start_hash;
int value = -1;
while (table->data[hash_value] != NULL) {
if (table->data[hash_value]->key == key) {
value = table->data[hash_value]->value;
break;
}
hash_value = rehash(hash_value);
if (hash_value == start_hash) {
break;
}
}
return value;
}
int main() {
struct hash_table table;
for (int i = 0; i < SIZE; i++) {
table.data[i] = NULL;
}
put(&table, 1, 10);
put(&table, 2, 20);
put(&table, 3, 30);
printf("%d\n", get(&table, 1));
printf("%d\n", get(&table, 2));
printf("%d\n", get(&table, 3));
printf("%d\n", get(&table, 4));
return 0;
}
```
这个散列表使用取余法计算哈希值,并且使用线性探测法来解决冲突。在 `put` 函数中,如果发生冲突,就不断向后查找下一个空位置,直到找到一个空位置或者查找完整个散列表。在 `get` 函数中,先计算出哈希值,然后依次向后查找,直到找到目标元素或者查找完整个散列表。注意,我们在每次 `put` 操作时都需要动态分配一个新的节点,以便保存键值对信息。