當前位置:首頁 » 編程語言 » c語言散列表

c語言散列表

發布時間: 2022-05-31 08:08:55

A. c語言 有關散列表裝填因子的疑問

  1. 下標是從0~m-1,表長是m

  2. 要散列元素的個數,是

  3. 填裝因子:

  4. 散列表中的元素個數與散列表大小的比值。

填裝因子在各個散列方式中有不同的要求,它的值對散列表的性能有至關重要的影響。

在分離鏈式法中,要獲得好的效率,要求填裝因子約等於1。

而在線性探測法和平發探測法中,要獲得好的效率,要求填裝因子>0.5。

表大小為素數,也有助於散列表獲得更好的性能。

有可能

B. c語言散列表輸出文件有問題,求大神指導

Iskeyword_print(int key);
Notkey_print(char *word,char*keyword);
中不能寫int key,應為一個指針。
char *word,char*keyword 應為兩個指針

C. 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;

}

D. c語言數據結構,哈希表,我有個關於哈希表的問題,剛學哈希表,還不太清楚他好在哪裡

如果編號對應的記錄的數量不止一個,是還得遍歷,但遍歷的記錄的數量明顯減少了呀!因為總的記錄已經分到了多個不同的編號下面。

E. 散列表的設計c語言實現

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const int HASH_TABLE_SIZE = 13;

typedef struct hash_table_pair_s {
char *key;
int value;
struct hash_table_pair_s *next;
} hash_table_pair_t;

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_table_insert(hash_table_pair_t *hash_table, const char *key, int value)
{
int pos;
hash_table_pair_t *new_pair;
char *new_str;

pos = ELFhash(key) % HASH_TABLE_SIZE;
if (hash_table[pos].key != NULL) {
printf("conflict: %s and %s\n", key, hash_table[pos].key);
new_pair = (hash_table_pair_t *)malloc(sizeof(hash_table_pair_t));
new_str = (char *)malloc(sizeof(char) * (strlen(key) + 1));
strcpy(new_str, key);
new_pair->key = new_str;
new_pair->value = value;
new_pair->next = hash_table[pos].next;
hash_table[pos].next = new_pair;
}
else {
new_str = (char *)malloc(sizeof(char) * (strlen(key) + 1));
strcpy(new_str, key);
hash_table[pos].key = new_str;
hash_table[pos].value = value;
hash_table[pos].next = NULL;
}
}

int hash_table_get(hash_table_pair_t *hash_table, const char *key, int *value)
{
int pos;
hash_table_pair_t *p;

pos = ELFhash(key) % HASH_TABLE_SIZE;
for (p = &hash_table[pos]; p != NULL; p = p->next) {
if (!strcmp(p->key, key)) {
*value = p->value;
return 0;
}
}
return -1;
}

int main()
{
int i, status, value;
const char *MyBirds[13] = { "robin", "sparrow", "hawk", "eagle", "seagull", "bluejay", "owl", "cardinal", "Jakana", "Moa", "Egret", "Penguin", "hawk" };

hash_table_pair_t *hash_table = (hash_table_pair_t *)malloc(HASH_TABLE_SIZE * sizeof(hash_table_pair_t));
for (i = 0; i < HASH_TABLE_SIZE; i++) {
hash_table[i].key = NULL;
hash_table[i].next = NULL;
hash_table[i].value = 0;
}
for (i = 0; i < sizeof(MyBirds) / sizeof(MyBirds[0]); i++) {
hash_table_insert(hash_table, MyBirds[i], i);
}
for (i = 0; i < sizeof(MyBirds) / sizeof(MyBirds[0]); i++) {
status = hash_table_get(hash_table, MyBirds[i], &value);
if (status == -1) {
printf("not found %s\n", MyBirds[i]);
}
else {
printf("found %s: %d\n", MyBirds[i], value);
}
}

return 0;
}

F. 散列表的設計(C語言)

菜鳥不會
高手懶得做

G. 用c語言如何建立一個散列表能不能給個簡單的代碼解釋一下

//來源http://hi..com/xiaojiang/item/6a0a4d14430509f4dceecae5
#include <stdio.h>
#include <stdlib.h>
//定義哈希函數 num :需要進行散列的數據
/*author 碼農小江*/
int hashFunc( int num)
{
int hashValue;
hashValue = num%7;
return hashValue;
}

void main()
{
int i, j,k, hash_value, second_hash;
static int hashTable[7];//定義長度是7的散列表
int a[6] = {38,25,74,63,52,48};//線性表
for(i=0; i<6; i++)
{
hash_value = hashFunc(a[i]);//計算每個元素的散列值
if(!hashTable[hash_value])//若沒有沖突,即當前第一次佔用則成功
{
hashTable[hash_value] = a[i];
}else//否則重新計算,到沒有沖突為止
{
for(j=1; j<6; j++)
{
second_hash = (hash_value + j)%7;
if(!hashTable[second_hash])
{
hashTable[second_hash] = a[i];
break;
}
}
}
}

for(k=0;k<7;k++)
{
printf("%d\n", hashTable[k]);
}
}

H. C語言的映射是什麼

書的後面不是有講,散列表就是一種映射。
數據的存儲方式是按照key<->value。
key和value有種映射關系。
在散列表中key則是通過散列函數計算出來的。

I. C語言 運用散列函數拼寫檢查

可以考慮建一個桶式的散列表,表的主體結構是個長為26*27的指針數組,每個指針分別指向一個子鏈表,每個鏈表分別存放開頭為a,aa,ab,ac......g,ga,gb,......zy,zz為單詞.(不區分大小寫)
這樣的話,假設單詞存放在字元串str中,則散列函數就是
h(str)=(str[0]-'A')*27+str[1]-'A'

從題目給的散列函數就可以明顯看出,散列表肯定要用長度為HASHSIZE的數組啊,你的鏈表設想肯定不符合題目要求

J. c語言 簡單哈希表

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

熱點內容
python做腳本 發布:2025-02-11 17:05:42 瀏覽:548
風神瞳腳本 發布:2025-02-11 17:02:18 瀏覽:690
物理化學壓縮 發布:2025-02-11 17:02:03 瀏覽:295
蔚來配置哪些值得加 發布:2025-02-11 16:58:28 瀏覽:325
索引型資料庫 發布:2025-02-11 16:58:26 瀏覽:916
hbasephp 發布:2025-02-11 16:44:41 瀏覽:761
微軟不給源碼 發布:2025-02-11 16:13:37 瀏覽:38
php的get方法 發布:2025-02-11 16:12:30 瀏覽:967
源碼網嘉 發布:2025-02-11 16:07:06 瀏覽:192
免費ftp服務軟體 發布:2025-02-11 15:58:06 瀏覽:866