Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的分离链_C - Fatal编程技术网

C语言中的分离链

C语言中的分离链,c,C,我正在编写一段代码,在C中使用单独的链接,以便在发生冲突时在哈希表的同一索引上存储两个或多个值。但现在我不知道如何将多个值放在同一个哈希表索引上。 下面的代码删除同一索引中最早的值,只获取新值。 我错过了什么 void ht_set( hashtable_t *hashtable, char *key, char *value ) { int bin = 0; entry_t *newpair = NULL;// entry_t *next = NULL; entry_t *last = NUL

我正在编写一段代码,在C中使用单独的链接,以便在发生冲突时在哈希表的同一索引上存储两个或多个值。但现在我不知道如何将多个值放在同一个哈希表索引上。 下面的代码删除同一索引中最早的值,只获取新值。 我错过了什么

void ht_set( hashtable_t *hashtable, char *key, char *value ) {
int bin = 0;
entry_t *newpair = NULL;//
entry_t *next = NULL;
entry_t *last = NULL;

bin = ht_hash( hashtable, key );//function to calculate hash index value

next = hashtable->table[ bin ];

while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
last = next;
next = next->next;
}

/* There's already a pair. Let's replace that string. */
if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {

free( next->value );
next->value = strdup( value );

/* Nope, could't find it. Time to grow a pair. */
} else {
newpair = ht_newpair( key, value );

/* We're at the start of the linked list in this bin. */
if( next == hashtable->table[ bin ] ) {
newpair->next = next;
hashtable->table[ bin ] = newpair;
/* We're at the end of the linked list in this bin. */
} else if ( next == NULL ) {
last->next = newpair;
/* We're in the middle of the list. */
} else {
newpair->next = next;
last->next = newpair;
}
}
}
这是我的结构

struct entry_s {
char *key;
char *value;
struct entry_s *next;
};

typedef struct entry_s entry_t;

struct hashtable_s {
int size;
struct entry_s **table;
};

typedef struct hashtable_s hashtable_t; 

抱歉,您的代码不太清楚,这是我的版本:

struct node {
  char* key,*value;
  node* next;
} *hashtable[table_size];

void AddNode(char* key,char* value)
  {
    struct node* newnode=malloc(sizeof (struct node));
    node->key=key;node->value=value;
    node->next=hashtable[hash(key)];
    hashtable[hash(key)]=node;
  }

struct node* FindNode(char* key)
  {
    struct node* node=hashtable[hash(key)];
    while(node!=NULL&&strcmp(key,node->key)!=0) node=node->next;
    return node;
  }

如果您还需要从表中删除,请将代码更改为使用双链接列表。

但如果发生冲突怎么办?我的意思是如果两个或更多的值在同一个散列索引上?您的代码将删除哈希表索引上最旧的值,并还原最新的值。我错了吗?不,不会的。它会简单地将新成员添加到链表“bunch”中。我已经写了这篇文章。它会不断删除旧值并只返回新值。您应该使用Function FindNode来获取值,而不是从数组中提取值!!!你不明白什么是链锁!冲突是指两个不同的键具有相同的散列。您得到的不是冲突,而是覆盖,当然它不能用任何方法解决。顺便说一下,我们的实现不处理覆盖-不要使用它们!世界上没有任何算法可以帮助你实现这样的覆盖。链接的用途是:ht_集(表,“键1”、“1”);ht_套装(表“koy1”、“foo”)//其中hash(key1)=hash(koy1)。这称为冲突,将被解决:ht_get(“key1”)=“1”;你得到(“koy1”)=“foo”;!!!你所做的永远不会成功。