當前位置:首頁 » 編程語言 » 哈希表的實現c語言代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

哈希表的實現c語言代碼

發布時間: 2022-05-12 12:48:29

Ⅰ 構造哈希表存儲電話號碼,用再哈希法處理沖突要c語言程序代碼

給你個hash表的介面和實現
你根據需求該下吧
#ifndef _Hash_H_
#define _Hash_H_
/*
* 查找演算法時間是 O(1)
* 優點:查找迅速
* 缺點:比較佔用內存
*/

/*
*
* hash原子結構體
*
*
*/
typedef struct hash_table_pair_s{
/*hash表長*/
int length;
/*hash key*/
char *key;
/*儲存的值,可以是地址*/
int value;
struct hash_table_pair_s *next;
}hash_table;

/*
* function: hash_insert
* --------------------------------------
* 往hash表中增加一個元素
* Usage:
*
* #include "corelib/hash.h"
* void run(hash_table*hash){
* int i, status, value;
* status = hash_get(hash,"hawk", &value);
* if(status!=-1){
* printf("find,%d",value);
* }else{
* printf("not find");
* }
* }
* void main(){
* hash_table* hash= hash_create(10);//創建一個表長是10的hash表
* hash_insert(hash,"robin", 0);
* hash_insert(hash,"sparrow", 1);
* hash_insert(hash,"hawk", 2);
* hash_insert(hash,"jack", 3);
* hash_insert(hash,"seagull", 4);
* run(hash);
* }
*
*/
void hash_insert(hash_table *hash,const char *key, int value);

/*
* function: hash_get
* --------------------------------------
* 根據key值在hash表中獲取一個元素對象
*
*
*/
int hash_get(hash_table *hash,const char* key, int *value);

/*
* function: hash_create
* 創建一個hash表
*
*/
hash_table* hash_create(int size);

#include "corelib/hash.c"
#endif
//-----------------------------------------------------------------------------------------------
#include <default.c>
#include <stdio.h>
/*
*一個key字元串佔用的位元組數
*/
#define KEY_SIZE 64

/*
*
*hash函數
*
*/
int ELFhash(const char *key){
unsigned long h = 0;
unsigned long g;
while( *key )
{
h =( h<< 4) + *key++;
g = h & 0xf0000000L;
if( g ) h ^= g >> 24;
h &= ~g;
}
return h;
}
void hash_insert(hash_table *hash,const char *key, int value){
char keystr[KEY_SIZE];
memset(keystr,0,KEY_SIZE);
memcpy(keystr,key,strlen(key));

int pos;
hash_table *new_pair;
char *new_str;
pos = ELFhash(key) % hash[0].length;

if (hash[pos].key != NULL) {
/*發生沖突*/
char log[512];
memset(log,0,512);
int j=0;
j+=sprintf(log+j,"沖突: %s and %s\n", keystr, hash[pos].key);
new_pair = (hash_table *)malloc(sizeof(hash_table));
new_str = (char *)malloc(sizeof(char) * (strlen(keystr) + 1));
strcpy(new_str, keystr);
new_pair->key = new_str;
new_pair->value = value;
new_pair->next = hash[pos].next;
hash[pos].next = new_pair;
// printf("%s",log);
}else{
int len = sizeof(char) * (strlen(keystr) + 1);
new_str = (char *)malloc(len);
memset(new_str,0,len);
strcpy(new_str, keystr);
hash[pos].key = new_str;
hash[pos].value = value;
hash[pos].next = NULL;
}
}
int hash_get(hash_table *hash,const char* key, int *value){
int pos;
hash_table *p;

char keystr[KEY_SIZE];
memset(keystr,0,KEY_SIZE);
memcpy(keystr,key,strlen(key));
pos = ELFhash(key) % hash[0].length;
for (p = &(hash[pos]); p != NULL; p = p->next) {
if (!strcmp(p->key, keystr)) {
*value = p->value;
return 0;
}
}
return -1;
}
hash_table* hash_create(int size){
int i;
hash_table *hash = (hash_table *)malloc(size * sizeof(hash_table));
hash[0].length = size;

for (i = 0; i < size; i++) {
hash[i].key = NULL;
hash[i].next = NULL;
hash[i].value = 0;
}
return hash;
}

#undef KEY_SIZE

Ⅱ c語言 簡單哈希表

哈希,其實是一種數據結構。
比如你拿到一組數據,你需要將這些數據一一存儲到你定義的數組中,假如現在用的是哈希的方式去存儲的,而不是順序存儲,那麼,就會出現一個哈希函數,(數值/一個質數),得到的結果就是應該存儲在數組中的下標值,這樣就可以使得分布很平均。

Ⅲ 可以不學數據結構直接學哈希表嗎C語言實現

可以的,哈希表那部分和圖,樹聯系不是很大。直接看是完全可以的,而且哈希這部分也比較容易些。

Ⅳ 用哈希表實現C語言關鍵字的演算法

#include <iostream.h>
#include<fstream.h>
#include <string>
#include <iomanip.h>
using namespace std;

const int TOTAL=32;
const int MAXLEN=10;
const int HASHLEN=41;
int cont=0;
class HashTable
{
public:
char keyword[MAXLEN];
int count;
int num;
};
HashTable HS[HASHLEN];

char KeyWords[TOTAL][MAXLEN]= {
"char", "double", "enum", "float", "int", "long", "short", "signed",
"struct", "union", "unsigned", "void", "break", "case", "continue",
"default", "do", "else", "for", "goto", "if", "return", "switch", "while",
"auto", "extern", "register", "static", "const", "sizeof", "typedef", "volatile"
};

template<class T>
class HASH
{
public:
void Show(int key);
int CreatHX(char *keyword);
int GetFreePos(int key);
void ResetHX();
int GetKey(char *keyword);
int isKeyWords(char *word);
int Read(char *filename);
int isLetter(char ch);
int FindHX(char *keyword);
private:
int key;
char *keyword;
char *word;
char ch;

};
template<class T>
void HASH<T>::Show(int key)
{
if(key<0||key>=HASHLEN)
{
cout<<"關鍵字不存在!"<<endl;
return;
}
if(strlen(HS[key].keyword)==0)
{
cout<<"哈希表位置:"<<key<<" 記錄是空的"<<endl;
return ;
}
cout<<"哈希表位置: "<<key<<" 關鍵字: "
<<HS[key].keyword<<" 出現次數 "<<HS[key].count<<endl;
cont++;
}

template<class T>
int HASH<T>::CreatHX(char *keyword)
{
int key;
if(!isKeyWords(keyword)) return -1;
key=GetKey(keyword);

if(strlen(HS[key].keyword)>0)
{
if(strcmp(HS[key].keyword,keyword)==0)
{
HS[key].count++;
return 1;
}
key=FindHX(keyword);
if(key<0)
{
key=GetFreePos(GetKey(keyword));
if(key<0) return -1;
strcpy(HS[key].keyword,keyword);
}

if(key<0) return -1;
HS[key].count++;
}
else
{
strcpy(HS[key].keyword,keyword);
HS[key].count++;
}
return 1;
}
template<class T>
int HASH<T>::GetFreePos(int key)
{
int find,tem=0;
if(key<0||key>=HASHLEN) return -1;
for(find=key+1;find<HASHLEN;find++)
{
tem++;
if(strlen(HS[find].keyword)==0){
HS[find].num=tem;
return find; }
}

for(find=0;find<key;find++)
{
tem++;
if(strlen(HS[find].keyword)==0){
HS[find].num=tem;
return find; }
}
return -1;
}

template<class T>
void HASH<T>::ResetHX()
{
int i;
for(i=0;i<HASHLEN;i++)
{
strcpy(HS[i].keyword,"");
HS[i].count=0;
HS[i].num=0;
}
}

template<class T>
int HASH<T>::GetKey(char *keyword)
{
return ( keyword[0]*100+keyword[strlen(keyword)-1] ) % 41;
}

template<class T>
int HASH<T>::isKeyWords(char *word)
{
int i;
for(i=0;i<TOTAL;i++)
if(strcmp(word,KeyWords[i])==0) return 1;
return 0;
}

template<class T>
int HASH<T>::isLetter(char ch)
{
if( (ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z') ) return 1;
else return 0;

}

template<class T>
int HASH<T>::FindHX(char *keyword)
{
int key,find,tem=0;
if(!isKeyWords(keyword)) return -1;
key=GetKey(keyword);
if(strcmp(HS[key].keyword,keyword)==0) return key;
for(find=key+1;find<HASHLEN;find++)
{
tem++;
if(strcmp(HS[find].keyword,keyword)==0){
HS[find].num=tem;
return find; }
}

for(find=0;find<key;find++)
{
tem++;
if(strcmp(HS[find].keyword,keyword)==0){
HS[find].num=tem;
return find; }
}
return -1;
}

template<class T>
int HASH<T>::Read(char *filename)
{
char word[MAXLEN],ch;
int i;
FILE *read;
fstream myfile;
myfile.open("filename");
if(!myfile)
{
cout<<"文件不存在,請確認好再輸入!"<<endl;
return -1;
}
ResetHX();
while(!feof(read))
{
i=0;
ch=fgetc(read);
while(isLetter(ch)==0 && feof(read)==0 )
ch=fgetc(read);
while(isLetter(ch)==1 && feof(read)==0 )
{
if(i==MAXLEN)
{
while(isLetter(ch)==1&& feof(read)==0)
{
ch=fgetc(read);
}
i=0;
break;
}

else
{
word[i++]=ch;
ch=fgetc(read);
}
}
word[i]='\0';
if(isKeyWords(word))
{
CreatHX(word);
}
}
fclose(read);
cout<<"讀取成功,文件中關鍵字已經存入hash表,請繼續操作"<<endl;
return 1;
}

void main()
{
HASH<char> hash;
char filename[128],word[MAXLEN];
int i=0,count=0;
int key;
char j,y;
cout<<"請輸入要讀取的文件名:";
cin>>filename;
cout<<endl;
hash.Read(filename);
for(i=0;i<HASHLEN;i++)
{
hash.Show(i);
getchar();
}
cout<<"關鍵字總數為:"<<cont<<endl;
cout<<"請輸入你想要查找的關鍵字: ";
cin>>word;
cout<<endl;
hash.Show(hash.FindHX(word));
cout<<" C語言中的關鍵字和其在哈希表的位置:"<<endl;
for(i=0;i<TOTAL;i++)
{
cout<<setiosflags(ios::left)<<"["<<setw(2)<<hash.GetKey(KeyWords[i])<<"]"
<<setiosflags(ios::left)<<setw(11)<<KeyWords[i];
if((i+1)%4==0) cout<<endl;
}
cout<<"是否顯示沖突關鍵字列表?y(是) 其它(否):";
cin>>j;
if(j=='y')
{
cout<<"沖突關鍵字列表"<<endl;
for(i=0;i<HASHLEN;i++)
{
if(strlen(HS[i].keyword)>0)
{
key=hash.GetKey(HS[i].keyword);
if(key!=i)
{
count++;
cout<<setiosflags(ios::left)<<setw(14)<<
"\t[關鍵 字]:"<<HS[i].keyword<<setiosflags(ios::left)<<
setw(20)<<"\t[哈希表當前位置]: "<<i<<setiosflags(ios::left)<<
setw(20)<<"\t[沖突次數]: "<<HS[i].num<<endl;
}
}
}
if(count==0)
cout<<"沒有沖突"<<endl;
else
cout<<"\t沖突關鍵字共:"<<count<<"個"<<endl;
}
else
cout<<"不顯示沖突關鍵字列表,但已存在!"<<endl;
return;
}

Ⅳ 哈希表的c語言程序。。

int ELFhash(char *key)
{ unsigned long h=0;
while(*key)
{ h=(h<<4)+*key++;
unsigned long g=h&0Xf0000000L;
if(g) h^=g>>24;
h&=~g;
}
return h%MOD;
}

Ⅵ 如何用c語言建立哈希表 存有序對

#include<ctype.h>
#include<malloc.h>
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
#define NULLKEY 0 // 0為無記錄標志
#define N 10 // 數據元素個數
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)<=(b))
typedef int Status; // Status是函數的類型,其值是函數結果狀態代碼,如OK等
typedef int Boolean; // Boolean是布爾類型,其值是TRUE或FALSE
typedef int KeyType; // 設關鍵字域為整型

struct ElemType // 數據元素類型
{
KeyType key;
int ord;
};

int hashsize[]={11,19,29,37}; // 哈希表容量遞增表,一個合適的素數序列
int m=0; // 哈希表表長,全局變數
struct HashTable
{
ElemType *elem; // 數據元素存儲基址,動態分配數組
int count; // 當前數據元素個數
int sizeindex; // hashsize[sizeindex]為當前容量
};

Status InitHashTable(HashTable &H)// 操作結果: 構造一個空的哈希表
{ int i;
H.count=0; // 當前元素個數為0
H.sizeindex=0; // 初始存儲容量為hashsize[0]
m=hashsize[0];
H.elem=(ElemType*)malloc(m*sizeof(ElemType));
if(!H.elem)
exit(OVERFLOW); // 存儲分配失敗
for(i=0;i<m;i++)
H.elem[i].key=NULLKEY; // 未填記錄的標志
return OK;
}

void DestroyHashTable(HashTable &H)// 初始條件: 哈希表H存在。操作結果: 銷毀哈希表H
{ free(H.elem);
H.elem=NULL;
H.count=0;
H.sizeindex=0;
}

unsigned Hash(KeyType K)// 一個簡單的哈希函數(m為表長,全局變數)
{ return K%m;
}

void collision(int &p,int d) // 線性探測再散列
{
p=(p+d)%m;// 開放定址法處理沖突
}

Status SearchHash(HashTable H,KeyType K,int &p,int &c)// 在開放定址哈希表H中查找關鍵碼為K的元素,若查找成功,以p指示待查數據
{ p=Hash(K); // 求得哈希地址
while(H.elem[p].key!=NULLKEY&&!EQ(K,H.elem[p].key))
{ // 該位置中填有記錄.並且關鍵字不相等
c++;
if(c<m)
collision(p,c); // 求得下一探查地址p
else
break;
}
if EQ(K,H.elem[p].key)
return SUCCESS; // 查找成功,p返回待查數據元素位置
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY),p返回的是插入位置
}

Status InsertHash(HashTable &,ElemType); // 對函數的聲明
void RecreateHashTable(HashTable &H) // 重建哈希表
{ int i,count=H.count;
ElemType *p,*elem=(ElemType*)malloc(count*sizeof(ElemType));
p=elem;
printf("重建哈希表\n");
for(i=0;i<m;i++) // 保存原有的數據到elem中
if((H.elem+i)->key!=NULLKEY) // 該單元有數據
*p++=*(H.elem+i);
H.count=0;
H.sizeindex++; // 增大存儲容量
m=hashsize[H.sizeindex];
p=(ElemType*)realloc(H.elem,m*sizeof(ElemType));
if(!p)
exit(OVERFLOW); // 存儲分配失敗
H.elem=p;
for(i=0;i<m;i++)
H.elem[i].key=NULLKEY; // 未填記錄的標志(初始化)
for(p=elem;p<elem+count;p++) // 將原有的數據按照新的表長插入到重建的哈希表中
InsertHash(H,*p);
}

Status InsertHash(HashTable &H,ElemType e)// 查找不成功時插入數據元素e到開放定址哈希表H中,並返回OK;
{ int c,p;
c=0;
if(SearchHash(H,e.key,p,c)) // 表中已有與e有相同關鍵字的元素
return DUPLICATE;
else if(c<hashsize[H.sizeindex]/2) // 沖突次數c未達到上限,(c的閥值可調)
{ // 插入e
H.elem[p]=e;
++H.count;
return OK;
}
else
RecreateHashTable(H); // 重建哈希表
return ERROR;
}

void TraverseHash(HashTable H,void(*Vi)(int,ElemType))// 按哈希地址的順序遍歷哈希表
{
printf("哈希地址0~%d\n",m-1);
for(int i=0;i<m;i++)
if(H.elem[i].key!=NULLKEY) // 有數據
Vi(i,H.elem[i]);
}

Status Find(HashTable H,KeyType K,int &p)// 在開放定址哈希表H中查找關鍵碼為K的元素,若查找成功,以p指示待查數據
{ int c=0;
p=Hash(K); // 求得哈希地址
while(H.elem[p].key!=NULLKEY&&!EQ(K,H.elem[p].key))// 該位置中填有記錄.並且關鍵字不相等
{ c++;
if(c<m)
collision(p,c); // 求得下一探查地址p
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY)
}
if EQ(K,H.elem[p].key)
return SUCCESS; // 查找成功,p返回待查數據元素位置
else
return UNSUCCESS; // 查找不成功(H.elem[p].key==NULLKEY)
}

void print(int p,ElemType r)//輸出
{
printf("address=%d (%d,%d)\n",p,r.key,r.ord);
}

void main()
{
ElemType r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
HashTable h;
int i,p;
Status j;
KeyType k;
InitHashTable(h);
for(i=0;i<N-1;i++)// 插入前N-1個記錄
{
j=InsertHash(h,r[i]);
if(j==DUPLICATE)
printf("表中已有關鍵字為%d的記錄,無法再插入記錄(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
}
printf("按哈希地址的順序遍歷哈希表:\n");
TraverseHash(h,print);
printf("請輸入待查找記錄的關鍵字: ");
scanf("%d",&k);
j=Find(h,k,p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("沒找到\n");
j=InsertHash(h,r[i]); // 插入第N個記錄
if(j==ERROR) // 重建哈希表
j=InsertHash(h,r[i]); // 重建哈希表後重新插入
printf("按哈希地址的順序遍歷重建後的哈希表:\n");
TraverseHash(h,print);
printf("請輸入待查找記錄的關鍵字: ");
scanf("%d",&k);
j=Find(h,k,p);
if(j==SUCCESS)
print(p,h.elem[p]);
else
printf("沒找到\n");
DestroyHashTable(h);
}

Ⅶ 數據結構 哈希表,C語言解答

#include <stdio.h>
#include<malloc.h>
#include<string.h>
//#include
#define HASH_LEN 50 //哈希表的長度
#define M 47
#define NAME_NO 30 //人名的個數
typedef struct NAME
{
char *py; //名字的拼音
int k; //拼音所對應的整數
}NAME;
NAME NameList[HASH_LEN];

typedef struct hterm //哈希表
{
char *py; //名字的拼音
int k; //拼音所對應的整數
int si; //查找長度
}HASH;
HASH HashList[HASH_LEN];
/*-----------------------姓名(結構體數組)初始化---------------------------------*/
void InitNameList()
{ int i;
char *f;
int r,s0;
NameList[0].py="chenghongxiu";
NameList[1].py="yuanhao";
NameList[2].py="yangyang";
NameList[3].py="zhanghen";
NameList[4].py="chenghongxiu";
NameList[5].py="xiaokai";
NameList[6].py="liupeng";
NameList[7].py="shenyonghai";
NameList[8].py="chengquan";
NameList[9].py="luqing";
NameList[10].py="gongyunxiang";
NameList[11].py="sunzhenxing";
NameList[12].py="sunrongfei";
NameList[13].py="sunminglong";
NameList[14].py="zhanghao";
NameList[15].py="tianmiao";
NameList[16].py="yaojianzhong";
NameList[17].py="yaojianqing";
NameList[18].py="yaojianhua";
NameList[19].py="yaohaifeng";
NameList[20].py="chengyanhao";
NameList[21].py="yaoqiufeng";
NameList[22].py="qianpengcheng";
NameList[23].py="yaohaifeng";
NameList[24].py="bianyan";
NameList[25].py="linglei";
NameList[26].py="fuzhonghui";
NameList[27].py="huanhaiyan";
NameList[28].py="liudianqin";
NameList[29].py="wangbinnian";

for (i=0;i<NAME_NO;i++)// *求出各個姓名的拼音所對應的整數
{
s0=0;
f=NameList[i].py;

for (r=0;*(f+r) != '\0';r++) //方法:將字元串的各個字元所對應的ASCII碼相加,所得的整數做為哈希表的關鍵字
s0=*(f+r)+s0;

NameList[i].k=s0;
}
}
/*-----------------------建立哈希表---------------------------------*/
void CreateHashList()
{int i;
for ( i=0; i<HASH_LEN;i++)//哈希表的初始化
{
HashList[i].py="";
HashList[i].k=0;
HashList[i].si=0;
}

for (i=0; i<NAME_NO;)
{
int sum=0;
int adr=(NameList[i].k) % M; //哈希函數
int d=adr;
if(HashList[adr].si==0) //如果不沖突
{
HashList[adr].k=NameList[i].k;
HashList[adr].py=NameList[i].py;
HashList[adr].si=1;
}
else //沖突
{
do
{
d=(d+((NameList[i].k))%10+1)%M; //偽散列
sum=sum+1; //查找次數加1
}while (HashList[d].k!=0);

HashList[d].k=NameList[i].k;
HashList[d].py=NameList[i].py;
HashList[d].si=sum+1;
}i++;
}
}

/*-------------------------------------查找------------------------------------*/
void FindList()
{ int r;
char name[20]={0};
int s0=0;
int sum=1;
int adr;
int d;
printf("\n\n請輸入姓名的拼音: "); //輸入姓名
scanf("%s",name);

for ( r=0;r<20;r++) //求出姓名的拼音所對應的整數(關鍵字)
s0+=name[r];

adr=s0 % M; //使用哈希函數
d=adr;

if(HashList[adr].k==s0) //分3種情況進行判斷
printf("\n姓名:%s 關鍵字:%d 查找長度為: 1",HashList[d].py,s0);
else if (HashList[adr].k==0)
printf("無該記錄!");
else
{
int g=0;
do
{
d=(d+s0%10+1)%M; //偽散列
sum=sum+1;
if (HashList[d].k==0)
{
printf("無記錄! ");
g=1;
}
if (HashList[d].k==s0)
{
printf("\n姓名:%s 關鍵字:%d 查找長度為:%d",HashList[d].py,s0,sum);
g=1;
}
}while(g==0);
}
}

/*--------------------------------顯示哈希表----------------------------*/
void Display()
{int i;
float average=0;
printf("\n\n地址\t關鍵字\t\t搜索長度\tH(key)\t\t拼音 \n"); //顯示的格式
for( i=0; i<15; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
// printf("按任意鍵繼續顯示...\n"); //由於數據比較多,所以分屏顯示(以便在Win9x/DOS下能看到所有的數據)
// getch();
for( i=15; i<30; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
// printf("按任意鍵繼續顯示...\n");
// getch();
for( i=30; i<40; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}
//printf("按任意鍵繼續顯示...\n");
//getch();
for( i=40; i<50; i++)
{
printf("%d ",i);
printf("\t%d ",HashList[i].k);
printf("\t\t%d ",HashList[i].si);
printf("\t\t%d ",(HashList[i].k)%M);
printf("\t %s ",HashList[i].py);
printf("\n");
}

for (i=0;i<HASH_LEN;i++)
{average+=HashList[i].si;
average/=NAME_NO;
printf("\n\n平均查找長度:ASL(%d)=%f \n\n",NAME_NO,average);
}
}
/*--------------------------------主函數----------------------------*/
void main()
{
/* ::SetConsoleTitle("哈希表操作"); //Windows API函數,設置控制台窗口的標題
HANDLE hCon = ::GetStdHandle(STD_OUTPUT_HANDLE); //獲得標准輸出設備的句柄
::SetConsoleTextAttribute(hCon, 10|0); //設置文本顏色
*/
printf("\n------------------------哈希表的建立和查找----------------------");
InitNameList();
CreateHashList ();

while(1)
{ char ch1;
printf("\n\n");
printf(" 1. 顯示哈希表\n");
printf(" 2. 查找\n");
printf(" 3. 退出\n");

err:

scanf("%c",&ch1);
if (ch1=='1')
Display();
else if (ch1=='2')
FindList();
else if (ch1=='3')
return;
else
{
printf("\n請輸入正確的選擇!");
goto err;
}
}
}

Ⅷ 哈希表的設計(數據結構C語言版)

解決沖突方法無非兩種嘛,而且這個不難的,幫你寫不是害你嘛,這個不學好以後的課程容易出現問題的!

Ⅸ 如何用C語言中實現哈希表

C++有 map,set
還有其他的,看STL相關的吧

數組還慢....

Ⅹ C語言實現哈希表的相關運算演算法 編寫程序實現哈希表的構造過程。

#define MaxSize 100 //定義最大哈希表長度
#define NULLKEY -1 //定義空關鍵字值
#define DELKEY -2 //定義被刪關鍵字值
typedef int KeyType; //關鍵字類型
typedef char * InfoType; //其他數據類型
typedef struct
{
KeyType key; //關鍵字域
InfoType data; //其他數據域
int count; //探查次數域
} HashData;

typedef HashData HashTable[MaxSize]; //哈希表類型

void InsertHT(HashTable ha,int &n,KeyType k,int p) //將關鍵字k插入到哈希表中
{
int i,adr;
adr=k % p;
if (ha[adr].key==NULLKEY || ha[adr].key==DELKEY) //x[j]可以直接放在哈希表中
{
ha[adr].key=k;
ha[adr].count=1;
}
else //發生沖突時採用線性探查法解決沖突
{
i=1; //i記錄x[j]發生沖突的次數
do
{
adr=(adr+1) % p;
i++;
}
while (ha[adr].key!=NULLKEY && ha[adr].key!=DELKEY);
ha[adr].key=k;
ha[adr].count=i;
}
n++;
}
void CreateHT(HashTable ha,KeyType x[],int n,