【发布时间】:2013-07-23 09:04:50
【问题描述】:
我正在尝试用链表实现一个哈希表,但有一件事不能正常工作。 首先我制作了链表代码并确保它按预期工作,所以我发现它必须与哈希表实现或它们之间的交互有关。
我可以添加几个对象并再次找到它们而没有问题。但是如果两个或多个对象使用相同的键进行哈希处理,它们应该创建一个链表,但由于某种原因,我只能在链表中找到最新添加的对象。
之前添加的对象在那里,因为我可以成功删除我无法使用“lookup_string”函数找到的对象,因此我知道该对象存储在该位置。
所以函数“lookup_string”应该传递我在哈希表中搜索的对象,而“搜索”函数是我只实现链表时使用的函数。值得一提的是,“搜索”功能用于删除过程,但不在我只想查找对象的过程中,我不确定这是否重要,因为我相信“查找字符串”应该能够找到没有“搜索”功能的对象。
所以为了简单起见,我存储了两个对象:
对象 1:
name(key) = bcd
telephone number = 123
对象 2:
name(key) = ace
telephone number = 910
现在对象 1 和 2 都将获得相同的哈希键值,因此它们将存储在哈希表的同一个槽中。他们应该创建一个链接列表,但是当我搜索对象(菜单中的选择号 3)时,我只能找到最新添加的对象,即对象 2。 但是我仍然可以删除对象 1,它会告诉我对象 1 已成功删除。
代码:
struct post
{
char name[30];
int tel;
struct post *next;
};
typedef struct post Post;
Post *head = NULL;
Post *current;
struct hash_table
{
Post **table;
int size;
};
typedef struct hash_table Post_table;
unsigned int Hash(Post_table *hash_table, char tempname[30])
{
int i;
int sum;
int key;
for(i = 0; i < strlen(tempname); i++)
{
sum += (int)tempname[i];
}
key = sum % hash_table->size;
return key;
}
Post_table *create_hash_table(int size)
{
int i;
Post_table *new_table;
if (size < 1)
{
return NULL;
}
//attempt to allocate memory for the table structure
if ((new_table = malloc(sizeof(Post_table))) == NULL)
{
return NULL;
}
//attempt to allocate memory for the table itself
if ((new_table->table = malloc(sizeof(Post *) * size)) == NULL)
{
return NULL;
}
//Initialize the elements of the table
for(i = 0; i < size; i++)
{
new_table->table[i] = NULL;
new_table->size = size;
}
return new_table;
}
Post *lookup_string(Post_table *hash_table, char tempname[30])
{
Post *list;
unsigned int hashkey = Hash(hash_table, tempname);
for(list = hash_table->table[hashkey]; list != NULL; list = list->next)
{
if (strcmp(tempname, list->name) == 0)
{
return list;
}
}
return NULL;
}
int add_string(Post_table *hash_table, char tempname[30], int temptel)
{
Post *new_list;
Post *current_list;
unsigned int hashkey = Hash(hash_table, tempname);
/* Attempt to allocate memory for list */
if ((new_list = malloc(sizeof(Post))) == NULL)
{
return 1;
}
/* Does item already exist? */
current_list = lookup_string(hash_table, tempname);
/* item already exists, don't insert it again. */
if (current_list != NULL)
{
return 2;
}
/* Insert into list */
printf("\nHash-key: %d\n", hashkey);
hash_table->table[hashkey] = AddList(tempname, temptel);
return 0;
}
Post* CreateList(char tempname[30], int temptel)
{
Post *ptr = (Post*)malloc(sizeof(Post));
strcpy(ptr->name, tempname);
ptr->tel = temptel;
ptr->next = NULL;
printf("\n creating list with headnode as [%s]\n",tempname);
head = current = ptr;
return ptr;
}
Post* AddList(char tempname[30], int temptel)
{
if (NULL == head)
{
return (CreateList(tempname, temptel));
}
printf("\n Adding node to end of list with value [%s]\n",tempname);
Post *ptr = (Post*)malloc(sizeof(Post));
strcpy(ptr->name, tempname);
ptr->tel = temptel;
ptr->next = NULL;
current->next = ptr;
current = ptr;
return ptr;
}
void skrivMeny(void)
{
printf("\n1: Register name and telephone number\n");
printf("2: Remove name and telephone number\n");
printf("3: Search for name\n");
printf("5: Exit\n");
}
Post* Search(char tempname[30], Post **prev)
{
Post *ptr = head;
Post *tmp = NULL;
int found = 0;
char structname[sizeof(tempname)];
printf("\n Searching the list for value [%s] \n",tempname);
while(ptr != NULL)
{
if (strcmp(ptr->name, tempname) == 0)
{
found = 1;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
if(found == 1)
{
if(prev)
{
*prev = tmp;
}
return ptr;
}
else
{
return NULL;
}
}
void free_entry(Post_table *hash_table, char tempname[30])
{
Post *del_list;
Post *temp;
int ret = 0;
unsigned int hashkey = Hash(hash_table, tempname);
del_list = lookup_string(hash_table, tempname);
ret = Delete(tempname);
if(ret != 0)
{
printf("\n delete [name = %s] failed, no such element found\n",tempname);
}
else
{
printf("\n delete [name = %s] passed \n",tempname);
}
}
int Delete(char tempname[30])
{
Post *prev = NULL;
Post *del = NULL;
printf("\n Deleting value [%s] from list\n",tempname);
del = Search(tempname,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
{
prev->next = del->next;
}
if(del == current && del != head)
{
current = prev;
}
else if(del == head)
{
head = del->next;
}
}
free(del);
del = NULL;
return 0;
}
int main()
{
printf("\nHej och välkommen till hashlistan\n\n");
int menyval = 1;
char tempname[30];
int temptel;
int key;
Post * ptr;
Post_table *hash_table;
int table_size = 10;
hash_table = create_hash_table(table_size);
while (menyval > 0 && menyval <= 5)
{
skrivMeny();
scanf("%d", &menyval);
if (menyval == 1)
{
printf("[Name] [Number] = ");
scanf("%s %d", &tempname[0], &temptel);//inmatning
add_string(hash_table, tempname, temptel);
}
if (menyval == 2)
{
printf("[Name] = ");
scanf("%s", &tempname[0]);
free_entry(hash_table, tempname);
}
if (menyval == 3)
{
printf("[Name] = ");
scanf("%s", &tempname[0]);
ptr = lookup_string(hash_table, tempname);
if(ptr == NULL)
{
printf("\n Search [name = %s] failed, no such element found\n",tempname);
}
else
{
printf("\n Search passed [name = %s tel = %d]\n",ptr->name, ptr->tel);
}
}
if (menyval == 5)
{
break;
}
}
return 0;
}
感谢所有有助于解决我的问题的帮助或提示!
编辑: 这就是我的代码现在的样子,我将文件分成两个文件; hash.c 和 lista.h 我在 c 文件中包含了头文件。
lista.h:
struct post
{
char name[30];
int tel;
struct post *next;
};
typedef struct post Post;
struct list
{
Post *head = NULL;
Post *current;
};
typedef struct list List;
Post* CreateList(char tempname[30], int temptel)
{
Post *ptr = (Post*)malloc(sizeof(Post));
strcpy(ptr->name, tempname);
ptr->tel = temptel;
ptr->next = NULL;
printf("\n creating list with headnode as [%s]\n",tempname);
return ptr;
}
Post* AddList(char tempname[30], int temptel, int emptyElement)
{
if (emptyElement == 1)
{
return (CreateList(tempname, temptel));
}
printf("\n Adding node to end of list with value [%s]\n",tempname);
Post *ptr = (Post*)malloc(sizeof(Post));
strcpy(ptr->name, tempname);
ptr->tel = temptel;
ptr->next = NULL;
return ptr;
}
int Delete(Post_table *hash_table, char tempname[30])
{
Post *prev = NULL;
Post *del = NULL;
printf("\n Deleting value [%s] from list\n",tempname);
del = Search(tempname,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
{
prev->next = del->next;
}
if(del == hash_table->table[hashkey].current && del != hash_table->table[hashkey].head)
{
hash_table->table[hashkey].current = prev;
}
else if(del == hash_table->table[hashkey].head)
{
hash_table->table[hashkey].head = del->next;
}
}
free(del);
del = NULL;
return 0;
}
hash.c:
struct hash_table
{
List *table;
int size;
};
typedef struct hash_table Post_table;
unsigned int Hash(Post_table *hash_table, char tempname[30])
{
int i;
int sum;
int key;
for(i = 0; i < strlen(tempname); i++)
{
sum += (int)tempname[i];
}
key = sum % hash_table->size;
return key;
}
Post_table *create_hash_table(int size)
{
int i;
Post_table *new_table;
if (size < 1)
{
return NULL;
}
//attempt to allocate memory for the table structure
if ((new_table = malloc(sizeof(Post_table))) == NULL)
{
return NULL;
}
//attempt to allocate memory for the table itself
if ((new_table->table = malloc(sizeof(Post *) * size)) == NULL)
{
return NULL;
}
//Initialize the elements of the table
for(i = 0; i < size; i++)
{
new_table->table[i] = NULL;
new_table->size = size;
}
return new_table;
}
Post *lookup_string(Post_table *hash_table, char tempname[30])
{
Post *list;
unsigned int hashkey = Hash(hash_table, tempname);
for(list = hash_table->table[hashkey]; list != NULL; list = list->next)
{
if (strcmp(tempname, list->name) == 0)
{
return list;
}
}
return NULL;
}
int add_string(Post_table *hash_table, char tempname[30], int temptel)
{
int emptyElement = 0;
Post *new_list;
Post *current_list;
unsigned int hashkey = Hash(hash_table, tempname);
/* Attempt to allocate memory for list */
if ((new_list = malloc(sizeof(Post))) == NULL)
{
return 1;
}
/* Does item already exist? */
current_list = lookup_string(hash_table, tempname);
/* item already exists, don't insert it again. */
if (current_list != NULL)
{
return 2;
}
/* Insert into list */
if (hash_table->table[hashkey] == NULL)
{
emptyElement = 1;
}
printf("\nHash-key: %d\n", hashkey);
hash_table->table[hashkey] = AddList(tempname, temptel);
if (emptyElement == 1)
{
hash_table->table[hashkey].head = hash_table->table[hashkey];
hash_table->table[hashkey].current = hash_table->table[hashkey];
}
if (emptyElement == 0)
{
hash_table->table[hashkey].current = hash_table->table[hashkey];
}
return 0;
}
void free_entry(Post_table *hash_table, char tempname[30])
{
Post *del_list;
Post *temp;
int ret = 0;
unsigned int hashkey = Hash(hash_table, tempname);
del_list = lookup_string(hash_table, tempname);
ret = Delete(tempname);
if(ret != 0)
{
printf("\n delete [name = %s] failed, no such element found\n",tempname);
}
else
{
printf("\n delete [name = %s] passed \n",tempname);
}
}
void skrivMeny(void)
{
printf("\n1: Register name and telephone number\n");
printf("2: Remove name and telephone number\n");
printf("3: Search for name\n");
printf("5: Exit\n");
}
Post* Search(char tempname[30], Post **prev)
{
Post *ptr = head;
Post *tmp = NULL;
int found = 0;
char structname[sizeof(tempname)];
printf("\n Searching the list for value [%s] \n",tempname);
while(ptr != NULL)
{
if (strcmp(ptr->name, tempname) == 0)
{
found = 1;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
if(found == 1)
{
if(prev)
{
*prev = tmp;
}
return ptr;
}
else
{
return NULL;
}
}
int main()
{
printf("\nHej och välkommen till hashlistan\n\n");
int menyval = 1;
char tempname[30];
int temptel;
int key;
Post * ptr;
Post_table *hash_table;
int table_size = 10;
hash_table = create_hash_table(table_size);
while (menyval > 0 && menyval <= 5)
{
skrivMeny();
scanf("%d", &menyval);
if (menyval == 1)
{
printf("[Name] [Number] = ");
scanf("%s %d", &tempname[0], &temptel);//inmatning
add_string(hash_table, tempname, temptel);
}
if (menyval == 2)
{
printf("[Name] = ");
scanf("%s", &tempname[0]);
free_entry(hash_table, tempname);
}
if (menyval == 3)
{
printf("[Name] = ");
scanf("%s", &tempname[0]);
ptr = lookup_string(hash_table, tempname);
if(ptr == NULL)
{
printf("\n Search [name = %s] failed, no such element found\n",tempname);
}
else
{
printf("\n Search passed [name = %s tel = %d]\n",ptr->name, ptr->tel);
}
}
if (menyval == 5)
{
break;
}
}
return 0;
}
但正如我所说,这部分似乎有问题:
struct list
{
Post *head = NULL;
Post *current;
};
由于这不正确,它会导致其他几个错误,所以我想先看看这部分有什么问题。
【问题讨论】:
-
您是否尝试过使用调试器单步执行查找功能,一次一行?您使用的是什么操作系统?什么编译器?
-
哦,我忘了说这是C语言,操作系统是最新的Ubuntu。它是用gcc编译的,我不记得是哪个版本。但是我现在无法访问我的计算机,并且我所在的这台计算机上没有编译器,这非常有限,所以我也无法安装编译器。
-
我的眼睛,我的眼睛!这是很多代码。
-
是的,我注意到了!我在写我的帖子时确实使用了意图,这会使代码更容易阅读,但不知何故,意图并不适用于最终帖子......
-
实际上,如果您使用 firefox,则意图会正确显示。而且我还更新了我的代码并将其分为一个 c 文件和一个头文件,其中头文件包含在 c 文件中。仍然是相同数量的代码,但可能更容易浏览。
标签: c list search linked-list hashtable