数据结构实验C语言实现散列表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验课题:做这个实验时采用Open Addressing框架,也可加做Separate Chaining以形成比较。
1 构造散列表,把字符串数组中的各项加入到散列表中
string MyBirds[13] = { "robin", "sparrow", "hawk", "eagle", "seagull", "bluejay", "owl", "cardinal", "Jakana", "Moa", "Egret", "Penguin", "hawk" };
用C表示,可以是
char * MyBirds[13] = { "robin", "sparrow", "hawk", "eagle", "seagull", "bluejay", "owl", "cardinal", "Jakana", "Moa", "Egret", "Penguin", "hawk" };
为便于观察冲突现象,初始构造散列表时,表的容量不要过大,对Open Addressing,装载因子为0.5左右,对于Separate Chaining,装载因子为1左右即可。也不要做rehash(应该改源代码的哪里,如何改)。
建议对源代码做些改动、增加一些输出(建议用条件编译控制这些输出),以便于观察冲突的发生和解决;
对于Open Addressing,参考代码的冲突解决方案是用的平方探测(quadratic probing),如果用线性探测(linear probing)的策略,应该对函数findPos做什么修改(冲突解决的策略都集中在那里)
#include
#include
#include "hashquad.h"
#include
#define MinTableSize 26
typedef unsigned int Index;
typedef Index Position;
struct HashTbl;
typedef struct HashTbl *HashTable;
enum KindOfEntry { Legitimate, Empty, Deleted };
struct HashEntry
{
char *Element;
enum KindOfEntry Info;
};
typedef struct HashEntry Cell;
struct HashTbl
{
int TableSize;
Cell *TheCells;
};
static int NextPrime( int N )
{
int i;
if( N % 2 == 0 )
N++;
for( ; ; N += 2 )
{
for( i = 3; i * i <= N; i += 2 )
if( N % i == 0 )
goto ContOuter;
return N;
ContOuter: ;
}
}
Index Hash( const char *Key, int TableSize )
{
return *Key % TableSize;
}
HashTable InitializeTable( int TableSize )
{
HashTable H;
int i;
/* 1*/ if( TableSize < MinTableSize )
{
/* 2*/ printf( "Table size too small" );
/* 3*/ return NULL;
}
/* Allocate table */
/* 4*/ H = (struct HashTbl *)malloc( sizeof( struct HashTbl ) );
/* 5*/ if( H == NULL )
/* 6*/ printf( "Out of space" );
/* 7*/ H->TableSize = NextPrime( TableSize );
/* Allocate array of Cells */
/* 8*/ H->TheCells = (struct HashEntry *)malloc( sizeof( Cell ) * H->TableSize );
/* 9*/ if( H->TheCells == NULL )
/*10*/ printf( "Out of space" );
/*11*/ for( i = 0; i < H->TableSize; i++ )
{
H->TheCells[i].Element=(char *)malloc(10*sizeof(char));
H->TheCells[i].Info=Empty;
}
/*12*/
/*13*/ return H;
}
Position Find( char *Key, HashTable H )
{
Position CurrentPos;
int CollisionNum;
/* 1*/ CollisionNum = 0;
/* 2*/ CurrentPos = Hash( Key, H->TableSize );
//printf("%d\n",CurrentPos);
/* 3*/ while( H->TheCells[ CurrentPos ].Info != Empty &&
strcmp(H->TheCells[ CurrentPos ].Element,Key)!=0 )
/* Probably need strcmp!! */
{
if (H->TheCells[ CurrentPos ].Element!= NULL)
printf("冲突: %s and %s\n", H->TheCells[ CurrentPos ].Element,Key);
/* 4*/ CurrentPos += 2 * ++CollisionNum - 1;
/* 5*/ if( CurrentPos >= H->TableSize )
/* 6*/ CurrentPos -= H->TableSize;
}
/* 7*/ return CurrentPos;
}
void Insert( char *Key, HashTable H )
{
Position Pos;
Pos = Find( Key, H );
if( H->TheCells[ Pos ].Info != Legitimate )
{
/* OK to insert here */
H->TheCells[ Pos ].Info = Legitimate;
strcpy(H->TheCells[ Pos ].Element,Key);
/* Probably need strcpy! */
}
}
/*char
*Retrieve( Position P, HashTable H )