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

哈希鏈表c語言

發布時間: 2022-01-17 05:08:50

① 請將其中的c語言輸出。(哈希表輸出)轉化成C++語言的 out輸出。並能正確運行

只有這個函數需要改,改為如下:
void Display() // 顯示哈希表
{ int i;
float average=0;

cout<<"\n地址\t關鍵字\t\t搜索長度\tH(key)\t 姓名\n"; //顯示的格式
for(i=0; i<50; i++)
{ cout<<i<<" ";
cout<<"\t"<<HashList[i].k<<" ";
cout<<"\t\t"<<HashList[i].si<<" ";
cout<<"\t\t"<<(HashList[i].k%M)<<" ";
cout<<"\t "<<HashList[i].py<<" ";
cout<<"\n";
}
for(i=0;i<HASH_LENGTH;i++)
average+=HashList[i].si;
average/=NAME_NO;
cout<<"平均查找長度:ASL("<<NAME_NO<<")="<<average<<endl;

}

② c語言從一個dict.txt讀取內容到文件建立哈希鏈表出現內存錯誤

把你的數據結構和insrt_list函數的代碼發一下

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

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

數組還慢....

④ C語言哈希表

/#include "iostream.h"
#include <iostream>
#include "string.h"
#include "fstream"
#define NULL 0
unsigned int key;
unsigned int key2;
int *p;
struct node //建節點
{
char name[8],address[20];
char num[11];
node * next;
};

typedef node* pnode;
typedef node* mingzi;
node **phone;
node **nam;
node *a;

using namespace std; //使用名稱空間

void hash(char num[11]) //哈希函數
{
int i = 3;
key=(int)num[2];

while(num[i]!=NULL)
{
key+=(int)num[i];
i++;
}
key=key%20;
}

void hash2(char name[8]) //哈希函數
{
int i = 1;
key2=(int)name[0];
while(name[i]!=NULL)
{
key2+=(int)name[i];
i++;
}
key2=key2%20;
}

node* input() //輸入節點
{
node *temp;
temp = new node;
temp->next=NULL;
cout<<"輸入姓名:"<<endl;
cin>>temp->name;
cout<<"輸入地址:"<<endl;
cin>>temp->address;
cout<<"輸入電話:"<<endl;
cin>>temp->num;
return temp;
}

int apend() //添加節點
{
node *newphone;
node *newname;
newphone=input();
newname=newphone;
newphone->next=NULL;
newname->next=NULL;
hash(newphone->num);
hash2(newname->name);
newphone->next = phone[key]->next;
phone[key]->next=newphone;
newname->next = nam[key2]->next;
nam[key2]->next=newname;
return 0;
}

void create() //新建節點
{
int i;
phone=new pnode[20];
for(i=0;i<20;i++)
{
phone[i]=new node;
phone[i]->next=NULL;
}
}
void create2() //新建節點
{
int i;
nam=new mingzi[20];
for(i=0;i<20;i++)
{
nam[i]=new node;
nam[i]->next=NULL;
}
}
void list() //顯示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}
void list2() //顯示列表
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=nam[i]->next;
while(p)
{
cout<<p->name<<'_'<<p->address<<'_'<<p->num<<endl;
p=p->next;
}
}
}

void find(char num[11]) //查找用戶信息
{
hash(num);
node *q=phone[key]->next;
while(q!= NULL)
{
if(strcmp(num,q->num)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"無此記錄"<<endl;
}
void find2(char name[8]) //查找用戶信息
{
hash2(name);
node *q=nam[key2]->next;
while(q!= NULL)
{
if(strcmp(name,q->name)==0)
break;
q=q->next;
}
if(q)
cout<<q->name<<"_" <<q->address<<"_"<<q->num<<endl;
else cout<<"無此記錄"<<endl;
}

void save() //保存用戶信息
{
int i;
node *p;
for(i=0;i<20;i++)
{
p=phone[i]->next;
while(p)
{
fstream iiout("out.txt", ios::out);
iiout<<p->name<<"_"<<p->address<<"_"<<p->num<<endl;
p=p->next;
}
}
}

void menu() //菜單
{

cout<<"0.添加記錄"<<endl;
cout<<"3.查找記錄"<<endl;
cout<<"2.姓名散列"<<endl;
cout<<"4.號碼散列"<<endl;
cout<<"5.清空記錄"<<endl;
cout<<"6.保存記錄"<<endl;
cout<<"7.退出系統"<<endl;
}

int main()
{
char num[11];
char name[8];

create();
create2() ;

int sel;
while(1)
{
menu();
cin>>sel;
if(sel==3)
{ cout<<"9號碼查詢,8姓名查詢"<<endl;
int b;
cin>>b;
if(b==9)
{ cout<<"請輸入電話號碼:"<<endl;
cin >>num;
cout<<"輸出查找的信息:"<<endl;
find(num);
}
else
{ cout<<"請輸入姓名:"<<endl;
cin >>name;
cout<<"輸出查找的信息:"<<endl;
find2(name);}
}
if(sel==2)
{ cout<<"姓名散列結果:"<<endl;
list2();
}
if(sel==0)
{ cout<<"請輸入要添加的內容:"<<endl;
apend();
}
if(sel==4)
{ cout<<"號碼散列結果:"<<endl;
list();
}
if(sel==5)
{ cout<<"列表已清空:"<<endl;
create();
create2();
}
if(sel==6)
{ cout<<"通信錄已保存:"<<endl;
save();
}
if(sel==7) return 0;
}
return 0;

}

⑤ c語言hash函數有幾種

#include<stdio.h>#include<stdlib.h>//這里我自己設計一個hash演算法來快速查找一堆數字中相等的數字,這也許是最接近原理的演算法了//一個整數整除27後的來作為hash函數//定義一個保存實際數據的結構體節點structdata_node{intnum;intcount;structdata_node*next;};//定義一個結構體時hash表的一部分typedefstruct{intkey;//余數structdata_node*p;//鏈表的頭指針}hash_node;#defineHASH_SIZE27intdo_hash(intnum)//hash表來求余數,這樣就可以了{returnnum%HASH_SIZE;}//初始化//添加數字//更新數字//刪除數字//查找數字hash_nodeHashTable[HASH_SIZE];//這里申明一個hashtable的數組//初始化函數,需要做的事將key復制為null,將p指針指向null,返回一個頭指針來指向這個hashtablevoidInitHashTable(hash_node*HashTable)
{//進行參數的校驗for(inti=0;i<HASH_SIZE;i++)
{
HashTable[i].key=0;HashTable[i].p=NULL;}
}//保存到這個鏈表中//如果這個鏈表是空的話,就作為頭指針,如果這個鏈表不為空,則添加到吧數字添加到末尾intsavedata(structdata_node**head,intnum)
{structdata_node*tmp_p=*head;structdata_node*p=(structdata_node*)malloc(sizeof(structdata_node));if(p==NULL)return0;if(*head==NULL)
{
*head=p;p->count=1;p->num=num;p->next=NULL;}else//如果不為空,則這個時候應該添加到鏈表末尾{while(tmp_p!=NULL)//如果存在,則將這個節點的count加1就可以了{if(tmp_p->num==num)
{
free(p);++tmp_p->count;return0;}if(tmp_p->next==NULL)break;tmp_p=tmp_p->next;}

tmp_p->next=p;p->count=1;p->num=num;p->next=NULL;}return0;}//添加數字//將這個數字經過hash求出結果,然後再保存到相應的鏈表中//返回真或者假就可以了intadd_hash(hash_node*HashTable,intnum)
{intmod=do_hash(num);returnsavedata(&HashTable[mod].p,num);}intmain()
{intnum=100;hash_node*H=HashTable;InitHashTable(H);add_hash(H,num);add_hash(H,num);add_hash(H,3);add_hash(H,1);add_hash(H,4);//在這里我們可以發現一個好的hash函數是多麼的重要,如果hash函數不好造成很多沖突的話,效率並不會提高很多的,理想的情況是沖突幾乎沒有//這也就是設計hash函數的精髓所在return0;}

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

設計一個哈希表,哈希函數用除留余數法,用開放定址法、線性探測處理沖突,從文本文件讀入30個左右中文人名,每行一人,可用本班學生名單。要求:
1.成功的平均查找長度不超過3,能查找人名,能插入新人名,能顯示當前哈希表中每個元素,每行顯示6個。
2.編寫主函數測試這些功能,並統計實際的平均查找長度。用菜單式操作。
3.如果有餘力,可以增加刪除功能(選做)。
提示:
根據成功的平均查找長度要求和估算公式求出裝填因子α上界及哈希表長度m。
#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沒有, 但第3方免費的有:
sglib -- 基於大量的宏,難調試

cbfalconer -- 現在已不更新和維護了

SunriseDD - 文檔比較全, 但框架太大

uthash - 這個效率很好, 但很多struct可能需要自己動手調整

glib (gnome hash) - 很好很完整, 但太大,太啰嗦了。

strmap - 簡單輕巧,但只能是字元串介面

libcfu - 這個相對不錯, 小巧簡單。
cmph - hash的簡單實現,也不錯。

你要是願意,Judy array也是很好的替代選擇, 這個其實是鏈表,但簡單,高效。

要是願意,自己寫一個也好, C Interfaces and Implementations這本書裡面有一個很好的例子。

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

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

⑨ c語言哈希表鏈地址法問題

結構體的成員x_和y_保存的是什麼?

你的

intp=Hash(cell);

找到所項後,因為該項中要有一個鏈保存一系列元素,所以該鏈應為一指向hash元素的指針。

判斷條件因為該項是否為NULL,若為NULL(地址不沖突),否則沖突,執行的是鏈表的插入操作,插入到前後首位隨你意願(一般插前邊)。

⑩ c語言 簡單哈希表

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