当前位置:首页 » 编程语言 » 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语言 简单哈希表

哈希,其实是一种数据结构。
比如你拿到一组数据,你需要将这些数据一一存储到你定义的数组中,假如现在用的是哈希的方式去存储的,而不是顺序存储,那么,就会出现一个哈希函数,(数值/一个质数),得到的结果就是应该存储在数组中的下标值,这样就可以使得分布很平均。

热点内容
c反编译工具re 发布:2025-02-11 10:26:37 浏览:673
光遇安卓怎么能加到ios 发布:2025-02-11 10:20:16 浏览:690
优势存储 发布:2025-02-11 10:20:14 浏览:362
光猫wifi怎么改密码 发布:2025-02-11 10:17:51 浏览:167
web和服务器怎么写通讯 发布:2025-02-11 10:08:06 浏览:979
安卓升级后手机变卡怎么办 发布:2025-02-11 09:58:01 浏览:113
土工数据库 发布:2025-02-11 09:48:55 浏览:963
libxml2编译 发布:2025-02-11 09:48:45 浏览:745
java类的复制 发布:2025-02-11 09:48:45 浏览:601
127小时ftp 发布:2025-02-11 09:47:10 浏览:852