C語言基于哈希表實現(xiàn)通訊錄
本文為大家分享了C語言基于哈希表實現(xiàn)通訊錄的具體代碼,供大家參考,具體內(nèi)容如下
1.需求分析
本演示程序用C語言編寫,完成哈希表的生成,電話號碼的插入、以及查找等功能。
(1)按提示輸入相應(yīng)的聯(lián)系人的相關(guān)資料;
(2)以相應(yīng)的輸出形式輸出所存儲的的聯(lián)系人的資料;
(3)程序可以達(dá)到建立、添加、查找、打印的功能;
(4)程序可以判斷用戶輸入的非法數(shù)據(jù)并引導(dǎo)正確的輸入。
2.概要設(shè)計
存儲電話號碼的記錄時,若在存儲位置和其關(guān)鍵字之間建立某種確定的對應(yīng)關(guān)系使得每個關(guān)鍵字和存儲結(jié)構(gòu)中一個唯一的存儲位置相對應(yīng),那么在進(jìn)行查找時,根據(jù)這個對應(yīng)關(guān)系f就可以找到給定值K的像f(K)。若存儲結(jié)構(gòu)中存在關(guān)接找到所查記錄。這個對應(yīng)關(guān)系f稱為哈希(Hash)函數(shù)或散列函數(shù)。按照以上思路建立的表稱為哈希表或散列表。本案例設(shè)計主要考察散列表的建立、查找和修改。。
3.詳細(xì)設(shè)計
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{
char num[11],name[15],address[20],city[15],etp[20];
struct node *next;
}NUM;
struct NUM *num_list[19];
int hash(char num[])
{
int i,k=0;
for(i=0;num[i]!='\0';i++)
{
k=10*k+num[i]-48; //字符轉(zhuǎn)化為數(shù)字
}
k=(k%19); //除余法求散列地址
return k;
}//c除留余數(shù)法處理電話號碼
void create()
{
struct node *p1;
int k1,m=0;
while(m==0)
{
printf("請輸入你想添加人的信息:num name address city etp,\n");
p1=(struct node *)malloc(sizeof(struct node));
scanf("%s",p1->num);
scanf("%s",p1->name);
scanf("%s",p1->address);
scanf("%s",p1->city);
scanf("%s",p1->etp);
k1=hash(p1->num);//用num數(shù)組值作為參數(shù)傳遞給哈希函數(shù)得到k1
p1->next=num_list[k1];//將k1得到的值作為數(shù)組的儲存地址賦值給頭結(jié)點的下一個節(jié)點
num_list[k1]=p1;//再將p1的數(shù)據(jù)傳遞給數(shù)組,故p1可以釋放作為下一個節(jié)點產(chǎn)生
printf("結(jié)束請按1,再次輸入請按0\n");
scanf("%d",&m);
}
printf("通訊表已經(jīng)創(chuàng)建\n");
}
void dlter()
{
char num[11];
int k1;
int find=0;
struct node *f;
printf("請查詢要修改的聯(lián)系人的電話:\n:");
scanf("%s",num);
k1=hash(num);
f=num_list[k1];
while(f!=NULL)
{
if(strcmp(f->num,num)==0)
{
printf("查找到了!請輸入要修改的人的資料:\n");
scanf("%s%s%s",f->num,f->name,f->address,f->city,f->etp);
find=1;
}
f=f->next;
}
if(find=0)
printf("沒有找到要刪除的節(jié)點!");
}
void list()
{
struct node *f;//打印節(jié)點指針
int i;
printf("打印通訊錄如下:\n");
for(i=0;i<19;i++)
{
f=num_list[i];
while(f!=NULL)
{
printf("--->num:%s\t name:%s\t address:%s\t city:%s\t etp:%s\t \n",f->num,f->name,f->address,f->city,f->etp);
f=f->next;
}
}
}
void add()
{
char num[11],name[15],address[20],city[15],etp[20];
struct node *p1;
int k1;
printf("請輸入新添加的人的信息:電話 姓名 地址 城市 郵箱\n");
p1=(struct node *)malloc(sizeof(struct node));
scanf("%s%s%s",num,name,address);
strcpy(p1->num,num);
strcpy(p1->name,name);
strcpy(p1->address,address);
strcpy(p1->city,city);
strcpy(p1->etp,etp);
k1=hash(p1->num);
p1->next=num_list[k1];
num_list[k1]=p1;
printf("ok\n");
}
void search()
{
char num[11];
int k1;
int find=0;
struct node *f;
printf("請輸入查詢?nèi)说碾娫捥柎a:");
scanf("%s",num);
k1=hash(num);
f=num_list[k1];
while(f!=NULL)
{
if(strcmp(f->num,num)==0)
{
printf("所要查找的聯(lián)系人信息 :num:%s name:%s address:%s city:%s etp:%s\n",f->num,f->name,f->address,f->city,f->etp);
find=1;
}
f=f->next;
}
if(find=0)
printf("此聯(lián)系人沒有找到!");
}
void main()
{
int i;
char x;
for(i=0;i<19;i++)
{
num_list[i]=NULL;
}
while(1)
{
// system("cls");
printf("\n");
printf("★★★★★★★★★通訊錄★★★★★★★★★\n");
printf("★◆----------------------------------◆★\n");
printf("★| 1.建立 ?。颸n");
printf("★| |★\n");
printf("★| 2.查找 ?。颸n");
printf("★| ?。颸n");
printf("★| 3.添加 ?。颸n");
printf("★| ?。颸n");
printf("★| 4.修改 ?。颸n");
printf("★| |★\n");
printf("★| 5.打印 ?。颸n");
printf("★| ?。颸n");
printf("★| 6.結(jié)束 ?。颸n");
printf("★◆----------------------------------◆★\n");
printf("★★★★★★★★★★★★★★★★★★★★★\n");
// x=getchar();
scanf("%s",&x);
switch(x)
{
case '1': create();break;
case '2': search();break;
case '3': add();break;
case '4': dlter();break;
case '5': list();break;
case '6': return;
default:printf("請重新輸入;\n");
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++:構(gòu)造函數(shù),析構(gòu)函數(shù)詳解
今天小編就為大家分享一篇關(guān)于C++構(gòu)造函數(shù)和析構(gòu)函數(shù)的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2021-09-09
如何使用visual studio2019創(chuàng)建簡單的MFC窗口(使用C++)
這篇文章主要介紹了如何使用visual studio2019創(chuàng)建簡單的MFC窗口(使用C++),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

