C语言实现简单的哈希表

test.c

#include<stdlib.h>
#include<windows.h>
#include<assert.h>
#include"HashTable.h"


int main()
{
    test();
    system("pause");
    return 0;
}

HashTable.c

#include<assert.h>
#include"HashTable.h"

void HashTableInit(HashTable* ht)
{
    ht->_capacity = 51ul;
    ht->_tables = (HashTable*)malloc(sizeof(HashNode)*ht->_capacity);
    if (ht->_tables)
    {
        ht->_size = 0;

        for (size_t i = 0; i < ht->_capacity; ++i)
        {
            ht->_tables[i]._key = 0;
            ht->_tables[i]._status = EMPTY;
        }
    }
    else
    {
        return;
    }
}

int HashTablePosition(HashTable *ht,KeyType key)
{
    return key % (ht->_capacity);
}

void HashTableInsert(HashTable* ht, KeyType key)
{
    assert(ht);
    if (ht->_size > ht->_capacity)
    {
        return;
    }
    size_t index= 0;
    size_t cur = 0;
    size_t pos = HashTablePosition(ht, key);
    cur = pos;
    while (ht->_tables[pos]._status != EMPTY)
    {
        if (ht->_tables[pos]._status == EXITS)
        {
            index++;
            pos = cur + index*index;
        }
        else
        {
            ht->_tables[pos]._key = key;
            ht->_tables[pos]._status = EXITS;
            ht->_size++;
            return;
        }

    }
    ht->_tables[pos]._key = key;
    ht->_tables[pos]._status = EXITS;
    ht->_size++;
}

//int HashTableInsert(HashTable* ht, KeyType key, ValueType value);

HashNode* HashTableFind(HashTable* ht, KeyType key)
{
    assert(ht);
    size_t index = 0;
    size_t cur = 0;
    size_t pos = HashTablePosition(ht, key);
    cur = pos;
    while (ht->_tables[pos]._status != EMPTY)
    {
        if (ht->_tables[pos]._status != EXITS)
        {
            return NULL;
        }
        else
        {
            if (ht->_tables[pos]._key == key)
            {
                return (&(ht->_tables)[pos]);
            }
            else
            {
                index++;
                pos = cur + index*index;
            }
        }
    }
    return NULL;
}

int HashTableRemove(HashTable* ht, KeyType key)
{
    assert(ht);
    HashNode*cur = HashTableFind(ht,key);
    if (cur == NULL)
    {
        return 0;
    }
    else
    {
        cur->_status = DELETES;
        ht->_size--;
        return 1;
    }
}

int HashTableDestory(HashTable* ht)
{
    assert(ht);
    ht->_tables->_status = EMPTY;
    ht->_capacity = 0;
    ht->_size = 0;
    free(ht->_tables);
    ht->_tables = NULL;
}

void HashTablePrint(HashTable *ht)
{
    if (ht == NULL)
    {
        return;
    }
    size_t pos = 0;
    while (pos < ht->_capacity)
    {
        if (ht->_tables[pos]._status == EMPTY)
        {
            printf("[NULL] ");
        }
        else if (ht->_tables[pos]._status == DELETES)
        {
            printf("[DELETE:%d] ",ht->_tables[pos]._key);
        }
        else
        {
            printf("[EXITS:%d] ",ht->_tables[pos]._key);
        }
        pos++;
    }
}

void test()
{
    HashTable ht;
    HashTableInit(&ht);
    HashTableInsert(&ht, 2);
    HashTableInsert(&ht, 3);
    HashTableInsert(&ht, 9);
    HashTableInsert(&ht, 10);
    HashTablePrint(&ht);
    printf("\n");
    printf("\n");
    printf("%d ", HashTableRemove(&ht, 2));
    printf("\n");
    HashTablePrint(&ht);
    printf("\n");
    printf("\n");
    HashTableInsert(&ht, 53);
    HashTablePrint(&ht);
    printf("\n");
}

HashTable.h

#ifndef __HASHTABLE__H__
#define __HASHTABLE__H__

#include<stdio.h>

typedef int KeyType;
typedef int ValueType;

enum Status
{
    EMPTY,
    EXITS,
    DELETES,
};

typedef struct HashNode
{
    KeyType _key;
    ValueType _value;
    enum Status _status;
}HashNode;

typedef struct HashTable
{
    HashNode* _tables;
    size_t _size;
    size_t _capacity;
}HashTable;

void HashTableInit(HashTable* ht);
void HashTableInsert(HashTable* ht, KeyType key);
//int HashTableInsert(HashTable* ht, KeyType key, ValueType value);
HashNode* HashTableFind(HashTable* ht, KeyType key);
int HashTableRemove(HashTable* ht, KeyType key);
int HashTableDestory(HashTable* ht);

#endif __HASHTABLE__H__
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值