题目:利用哈希表设计快速电话号码查询系统
要求:
- 通过Internet或知网等工具查找文献,论述哈希表和哈希查找的相关应用综述,字数不少于1500字。
- 请你为自己手机的电话簿以电话号码作为关键字建立哈希表,然后依据电话号码进行哈希查找,并采用合适的冲突处理方法处理冲突。查找成功显示姓名与号码,查找失败则进行插入。电话簿初始从文本文档中逐个读入,然后动态生成哈希表。
- 采用C语言、C++、python等程序语言实现,要求具有简单的人机交互界面。
- 完成课程设计报告,要求涉及问题描述与分析、数据结构与算法设计,代码实现、结果分析等。
数据结构与算法课程设计报告
题目:
姓名:
学号:
班级:
一、 哈希查找技术应用综述(不少于1500字)
二、 哈希电话号码查询系统设计
2.1 问题的表述与分析
2.2 问题的设计
2.3 代码设计与注释
2.4 结果分析与总结
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
int hashCode(long long key) {
return key % SIZE;
}
struct DataItem *search(long long key) {
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
// not find : insert it
return NULL;
}
void insert(long long key,char* data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data =(char *)malloc(20*sizeof(char));
strcpy(item->data ,data);
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item) {
long long key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void display() {
int i = 0;
for(i = 0; i<SIZE; i++) {
if(hashArray[i] != NULL)
printf(" 手机号:%lld 人名:%s \n",hashArray[i]->key,hashArray[i]->data);
}
printf("\n");
}
void meun(){
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("★★★★★★★★★★★★★★★★★★★★★\n");
printf("请输入数字");
}
// 1.建立
void create(){
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
dummyItem->data = " ";
dummyItem->key = -1;
FILE *fp = NULL;
if((fp = fopen("AddressList.txt","r")) == NULL) //判断文件是否存在及可读
{
printf("电话文件不存在!");
}
while (!feof(fp)) //循环读取每一行,直到文件尾
{
long long key;
char *Tdata;
fscanf(fp,"%s %lld\n",Tdata,&key);
insert(key,Tdata);
}
fclose(fp);
}
// 2.查找
void searchPlus(){
long long key;
printf("请输入手机号码");
scanf("%lld",&key);
item = search(key);
if(item != NULL) {
printf("手机号的主人是: %s\n", item->data);
} else {
printf("手机号不存在\n");
printf("请输入姓名");
char *data;
scanf("%s",data);
insert(key,data);
printf("添加成功");
}
}
// 3.添加
void insertPlus(){
long long key;
char * data;
printf("请输入手机号码");
scanf("%lld",&key);
printf("请输入姓名");
scanf("%s",data);
insert(key,data);
printf("添加成功");
}
int main(){
int x;
while(1){
meun();
scanf("%d",&x);
switch(x){
case 1: create(); break;
case 2: searchPlus();break;
case 3: insertPlus();break;
case 4: display(); break;
case 5: return;
default:printf("请重新输入 \n");
}
}
return 0;
}