插入和删除链接列表

时间:2014-11-19 19:23:05

标签: c linked-list hashtable

这是一个哈希表实现。

我有插件有点工作,但如何返回链表?

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

我试图让哈希表成为一个全局变量,但编程会在我运行时强制执行。

#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;
}

1 个答案:

答案 0 :(得分:0)

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

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

然后用:

来调用它
theHash = hashtable_insert(theHash, "Wow!");