gpt4 book ai didi

c - 在链接列表中插入和删除

转载 作者:行者123 更新时间:2023-11-30 17:25:37 25 4
gpt4 key购买 nike

这是一个哈希表实现。

我的插入有点工作,但如何返回链接列表?

我知道删除尚未完成,但我理解这个概念,我的问题是返回调整后的列表。

我尝试将哈希表设置为全局变量,但当我运行它时,编程会强制执行。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>

struct node {
char * data;
struct node * next;
};

struct hashtable {
struct node ** table;
int size;
int nentries;
};

struct hashtable * hashtable_new(int size) {
struct hashtable * result;
result = malloc(sizeof(struct hashtable));
result -> size = size;
result -> nentries = 0;
result -> table = malloc(sizeof(struct node) * size);
int i = 0;

for (i = 0; i < result->size; i++) {
result->table[i] = NULL;
// result->table[i]->data = NULL;
}

return result;
}

unsigned hash_string(struct hashtable *this, char * str) {
unsigned hash = 0;
int i = 0;
for ( i = 0; str[i] != '\0'; i++ ) {
hash = hash * 37 + str[i];
}
//return hash;
return hash % this-> size;
}

void hashtable_free(struct hashtable * this) {
int i;
struct node *table_nodes, *current, *next;

for(i = 0; i<this->size; i++) {
table_nodes = this->table[i];
current = table_nodes;

while (current != NULL){
next = current->next;
free(current);
current = next;
}
this->table[i] = NULL;
}

free(&this->table);
free(&this->size);
free(&this->nentries);
free(this);
}

void hashtable_insert(struct hashtable * table, char * string) {
struct node * new_node;
unsigned index = hash_string(table, string);

if(table->table[index] == NULL) {
printf("\nIndex: %d", index);
new_node = malloc(sizeof(struct node));
new_node -> next = table->table[index];
new_node -> data = string;
printf("\nData: %s", new_node->data);
table -> table[index] = new_node;
table -> nentries++;
printf("\n");
} else {
new_node = malloc(sizeof(struct node));
new_node->data = string;
new_node->next = NULL;
struct node * current = table->table[index];
struct node * next;
int size = 1;

while (current != NULL) {
next = current->next;
//if(current->data == string){
//return;
//}
if(current-> next == NULL){

//last element in list
current->next = new_node;
table->nentries++;
size++;
printf("\nIndex: %d", index);
printf("\nSize: %d", size);
printf("\nData: %s", current->next->data);
printf("\n");
return;
}
current = next;
size++;
}
}
}

void remove_hash(struct hashtable * this, char * item) {
//unsigned index = hash_string(this, item);
}

int lookup(struct hashtable * this, char * item) {
struct node *temp;
unsigned int index = hash_string(this, item);

temp = this->table[index];
while(temp != NULL) {
// do something
printf("%s, ", temp->data);

if(temp->data == item) {
printf("found %s\n", temp->data);
}
temp = temp->next;
}
return 0;
}

void print(struct hashtable * this) {
int i = 0;
printf("\n Size %d \n", this->size);
if(this == NULL) {
printf("Please construct the hashtable");
return;
}

for (i = 0; i < this->size; i++) {
if(this->table[i] == NULL) {
printf("\n %d: <empty>", i);
} else {
printf("\n %d: %s ", i, this->table[i]->data);
if(this->table[i]->next != NULL) {
printf("%s ", this->table[i]->next->data);
}
}
}
}

int main(int argc, char **argv) {
//struct node *theNode;
struct hashtable *theHash;

theHash = hashtable_new(9);

hashtable_insert(theHash, "I");
hashtable_insert(theHash, "am");
hashtable_insert(theHash, "a");;
hashtable_insert(theHash, "fish");
hashtable_insert(theHash, "glub");
print(theHash);

hashtable_insert(theHash, "glub");
lookup(theHash, "I");

print(theHash);

//printf("\n\n\n");
hashtable_free(theHash);
//print(theHash);

return 0;
}

最佳答案

由于 C 不允许您通过引用传递,因此您可以尝试返回哈希表,然后使用 hashtable_insert 的结果重新分配变量:

struct hashtable *hashtable_insert(struct hashtable *table, char *string) {
// awesome code here
return current;
}

然后用以下方式调用它:

theHash = hashtable_insert(theHash, "Wow!");

关于c - 在链接列表中插入和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27025235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com