由于全是代码,注释比较清新,所以本片博文 没有论述
先新建头文件 HashTable.h
代码如下 :
#ifndef HashTable_H
#define HashTable_H
typedef struct Node
{
int data;
struct Node * next;
}Node;
typedef struct HashTable
{
Node * head;
Node * link ;
int size ;
}HashTable;
enum TrueOrFalse {False,True};
typedef int BOOL;
/*
初始个空HashTable
*/
HashTable * InitHashTable();
/*
往HashTable中加元素,value 相同则不加入HashTable
*/
BOOL Add(HashTable * hashTable,int value);
/*
遍历HashTable
*/
void Traverse(HashTable * hashTable,void (*visit)(int item));
/*
清空HashTable
*/
void ClearHashTable(HashTable * hashTable);
/*
销毁HashTable 空间
*/
void Destroy(HashTable * hashTable);
/*
判断HashTable是否包含value
*/
BOOL Contain(HashTable * hashTable,int value);
/*
递归Destroy所以Node 节点的内存
*/
void DestroyAllNode(Node * node);
/*
删除某个节点
*/
BOOL DelNode(HashTable * hashTable,int value);
#endif
新建HashTable.C
代码如下:
#include"HashTable.h"
#include<stdlib.h>
HashTable * InitHashTable()
{
HashTable * hashtable = (HashTable *)malloc(sizeof(HashTable));
hashtable->link=NULL;
hashtable->head=NULL;
hashtable->size = 0;
return hashtable==NULL?NULL:hashtable;
}
BOOL Add(HashTable * hashTable,int value)
{
if(NULL!=hashTable)
{
if(!Contain(hashTable,value))
{
Node * node = (Node *)malloc(sizeof(Node));
node->data =value;
if(NULL==hashTable->head && hashTable->size==0)
{
hashTable->link = node ;
hashTable->head = node ;
}
else
{
hashTable->link->next=node;
hashTable->link=node ;
}
node->next = NULL;
hashTable->size++;
return True;
}
}
return False;
}
BOOL Contain(HashTable *hashTable ,int value)
{
Node * head = hashTable->head;
int size;
int i ;
if(NULL!=hashTable)
{
if(NULL==hashTable->head && hashTable->size ==0)
return False;
size = hashTable->size;
for( i=0;i<size;i++)
{
if(head->data==value)
return True;
head=head->next;
}
}//end NULL!=hashTable
return False;
}
void ClearHashTable(HashTable * hashTable)
{
Node * head = hashTable->head ;
if(NULL==hashTable->head || hashTable->size==0)
return ;
DestroyAllNode(head);
hashTable->head = NULL;
hashTable->size = 0;
}
void DestroyAllNode(Node * node)
{
if(node->next != NULL)
{
DestroyAllNode(node->next);
}
free(node);
}
void Destroy(HashTable * hashTable)
{
ClearHashTable(hashTable);
free(hashTable);
hashTable=NULL;//防止产生野指针
}
void Traverse(HashTable * hashTable,void (*visit)(int item))
{
Node *head = hashTable->head ;
int size = hashTable->size;
int i;
if(NULL==hashTable)
return ;
if(NULL==visit)
return ;
while(NULL!=head)
{
visit(head->data);
head=head->next ;
}
}
BOOL DelNode(HashTable * hashTable,int value)
{
if(NULL!=hashTable)
{
Node * head = hashTable->head;
Node * front_head = NULL;
Node * temp_DEL ;
while(head!=NULL)
{
if(head->data==value)
{
temp_DEL=head;
if(NULL==front_head) //如果head 的前驱节点为空
{
hashTable->head = head->next ;
}
else if(NULL==head->next)//如果是尾节点
{
front_head->next=NULL;
}
else
{
front_head->next = head->next;
}
free(temp_DEL);
hashTable->size--;
return True;
}//end {determine whether head->data equals value}
front_head = head ;
head=head->next;
}//end while
return False;
}
return -1;//返回 -1 代表hashTable 为空
}
写好了HashTable 的头文件 ,并且实现了其中的函数。
那就来测试下吧 ;。
void Display(int item)
{
printf("%d ",item);
}
int main()
{
int C [10] = {1,4,23,5,2,3,5,5,6,1};
HashTable * hashTable = InitHashTable();
int i ;
printf("-----------------------\n");
for( i=0;i<10;i++)
{
Add(hashTable,C[i]);
}
Traverse(hashTable , Display);
putchar('\n');
DelNode(hashTable,6);
Traverse(hashTable , Display);
putchar('\n');
return 0;
}
附上运行结果插图 :
运行结果中不包含数组C中重复的项;
而且可以按照value 删除节点;
希望得到大家的指正 。。