【发布时间】:2021-03-15 18:15:20
【问题描述】:
我有一个函数(create)来创建一个hashmap。
我的 hashmap 大于 11 ,但这是一个示例,我正在搜索以使代码尽可能清晰。
hashmap 将在每个节点中包含一对 KEY-VALUE。我的目标是创建一个通用插入函数,我可以在其中从数组中插入int、char 等。
在我的示例中,我插入 Int Insert 函数作为参数 Table t(我在其中插入我之前创建的 int),两个指向我必须插入的值的指针,我必须插入值的位置在用户传递的 hashmap 和 compare 函数中,为了比较两个键,如果相等则返回 1。问题是:当我尝试打印哈希图 (print(t)) 时,它会在最后一对插入的每个位置返回。
输入 我的 arr[11] 和 arr2[11]
预期输出
key: 1,val: 11
key: 2,val: 10
key: 3,val: 9
key: 4,val: 8
key: 5,val: 7
key: 6,val: 6
key: 7,val: 5
key: 8,val: 4
key: 9,val: 3
key: 10,val: 2
key: 11,val: 1
实际输出
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
key: 11, val: 1
main.c
int compare(void *key,void *key2){
int k = *(int*)key;
int k2 = *(int*)key2;
if(k==k2){
return 1;
}else return 0;
}
int main(){
struct table*t = create(11);
int arr[11]={1,2,3,4,5,6,7,8,9,10,11};
int arr2[11]={11,10,9,8,7,6,5,4,3,2,1};
for(int p=0;p<11;p++){
int i = arr[p];
int *key= &i;
int i2 = arr2[p];
int *value= &i2;
insert(t,key,value,p, compare);
}
print(t);
function.c
struct node{
void* key;
void* val;
struct node *next;
struct node *prev;
};
struct table{
int size;
struct node **list;
};
struct table *create(int size){
struct table *t = (struct table*)malloc(sizeof(struct table));
t->size = size;
t->list = (struct node**)malloc(sizeof(struct node*)*size);
int i;
for(i=0;i<size;i++)
t->list[i] = NULL;
return t;
}
void insert(struct table *t,void* key,void* val,int pos, int(*comp)(void*, void*)){
struct node *list = t->list[pos];
struct node *newNode = (struct node*)malloc(sizeof(struct node));
struct node *temp = list;
while(temp){
if(((*comp)(temp->key, key))==1){
printf("%s", "key already create.");
return;
}
temp = temp->next;
}
newNode->next = t->list[pos];
newNode->key = key;
newNode->val = val;
if(list!=NULL){
list->prev = newNode;
}
t->list[pos] = newNode;
newNode->prev = NULL;
}
void print(struct table *t){
for (int i = 0; i < t->size; ++i) {
struct node *list = t->list[i];
while(list){
if(list->key!=NULL) {
printf("key: %d, val: %d\n",*(int*)list->key, *(int*)list->val);
}
list = list->next;
}
}
}
【问题讨论】:
标签: arrays c pointers generics hashmap