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__
C语言实现简单的哈希表
最新推荐文章于 2024-05-22 10:06:55 发布