一个哈希表的简单实现拉链法

环境:amd64  系统:gentoo  编译器:gcc version 4.5.3 (Gentoo 4.5.3-r2 p1.5, pie-0.4.7) 

这是一个数据结构课程设计的作品....

/*
*哈希表 除留余数 拉链法
*/
#include
#include

#define TableSize 71

typedef unsigned int Index;

typedef struct ListNode
{
	char element[20];
	struct ListNode *next;
}*Position;

typedef struct HashTbl
{
	int TblSize;
	 Position *TheLists;
	//struct  HashTbl *next;
}*HashTable;

void InitializeTable(HashTable Tbl[])
{
	int i;
	HashTable tmp;
	for(i=0;iTblSize=TableSize;
		Tbl[i]->TheLists=NULL;
	}
}

Index Hash(char Key[])
{
	int i=0;
	int b=0;
	while(Key[i]!='\0' )
	{
		b+=(((int)Key[i])*((int)Key[i]));
		i++;
	}//这段用于获取key值,可以根据自己要求修改
	return b%TableSize;
	//除留余数法。。。
}

void Insert(HashTable Tbl[])
{
	//printf("acftion1\n");
	Position p,newnode;
	newnode=(Position)malloc(sizeof(struct ListNode));
	int i=0;
	char k[20];
	Index key;
		scanf("%s",k);
		key=Hash(k);
		//printf("%d\n",key);
		while(k[i]!='\0' )
		{
			newnode->element[i]=k[i];
			i++;
		}
		newnode->next=NULL;
		//printf("%s\n",newnode->element);
	
		p=Tbl[key]->TheLists;
		if(!p)
			Tbl[key]->TheLists=newnode;
		while(p)
		{	
			//printf("%s\n",p->element);
			if(p->next==NULL)
			{
				p->next=newnode;
				//printf("OK!");
				return ;
			}
			else
				p=p->next;
		}
}

void Print(HashTable Tbl[])
{
	int i,j,k,l,m;
	k=0;
	l=0;
	m=0;
	double n,o;
	Position p;
	for(i=0;iTheLists;
		while(p)
		{
			printf("%d->%d->%-20s\t",i,j,p->element);
			p=p->next;
			k++;	//统计输出次数,既是数据记录集的总个数
			j++;	//同一位置上的输出个数,当i+1是要清零
			m+=j;
			
			
		}
		
		if(!j)
			printf("%d->%d->\t",i,j);
		else
			j--;
		l+=j;		//计算冲突的记录
		
		//printf("\t%d",j);
		printf("\n");
		
	}
	n=(double)l/(double)k;	o=(double)m/(double)k;
	//k为输入记录的总数, l为冲突的记录的总个数  m统计每一个记录的查找长度。
	//n为冲突率, o为平均查找长度。
	printf("**********\t%d\t%d\t%d\n",k,l,m);
	printf("**********\tConflict:%lf\tASL:%lf\n",n,o);
}

void  Search(HashTable Tbl[])
{
	Position p;
	char k[20];
	Index key;
	int j;
	
	scanf("%s",k);
	key=Hash(k);printf("%d\n",key);
	p=Tbl[key]->TheLists;
	if(p)
	{
	while(strcmp(p->element,k))
	{
		p=p->next;
		if(!p)
		{
			printf("No Item found!\n");
			return ;
		}
		j++;
	}
	printf("%d->%d->%s\n",key,j,p->element);
	}
	else
	{
		printf("No Item found!\n");
	}
}

void Destroy(HashTable Tbl[])  
{  
	int i;
	Position p;  
	Position temp;
	
	for(i=TableSize-1;i>=0;i--)  
	{
		//printf("%d\n",i);
		p=Tbl[i]->TheLists;
		while(p!=NULL)  
		{  
			temp=p->next;  
			free(p);  
			p=temp;  
		}
		free(Tbl[i]);  
	}
}

int main()
{
	HashTable T[TableSize];
	InitializeTable(T);
	int a,i;
	while(1)
	{
		
		printf("*******1.Insert data**********\n");
		printf("*******2.search data*********\n");
		printf("*******3.show add ***********\n");
		printf("*******4.exit*******************\n");
		printf("switich action:	");
		scanf("%d",&a);
		switch(a)
		{
			case 1:
			 	Insert(T);
			 	break;
			 
			case 2:
				Search(T);
			 	break;
			  
			case 3:
				Print(T);
				break;
			 
			case 4:
				Destroy(T);
				return ;
				break;
			 
			default:
				printf("Err! please check!\n");
				break;
		}
	}
	return 0;
}



你可能感兴趣的:(算法设计与分析)