#include <stdio.h>
#define MAXTABLESIZE 100000
#define ERROR -1
typedef int ElementType;
typedef int Index;
typedef Index Position;
typedef enum
{
Legitimate,
Empty,
Deleted
} EntryType;
typedef struct HashEntry
{
ElementType Data;
EntryType Info;
}Cell;
typedef struct TblNode
{
int TableSize;
Cell* Cells;
}*HashTable;
HashTable BuildTable();
Position Hash(ElementType Key, int TableSize)
{
return (Key % TableSize);
}
Position Find(HashTable H, ElementType Key)
{
int hashi = Hash(Key, H->TableSize);
for (int i = 0; i < H->TableSize; i++)
{
if (H->Cells[hashi].Info == Empty
|| H->Cells[hashi].Data == Key)
{
return hashi;
}
hashi = (hashi + 1) % H->TableSize;
}
return ERROR;
}
int main()
{
HashTable H;
ElementType Key;
Position P;
H = BuildTable();
scanf("%d", &Key);
P = Find(H, Key);
if (P == ERROR)
printf("ERROR: %d is not found and the table is full.\n", Key);
else if (H->Cells[P].Info == Legitimate)
printf("%d is at position %d.\n", Key, P);
else
printf("%d is not found. Position %d is returned.\n", Key, P);
return 0;
}
04-11
1198
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交