利用哈希表设计快速电话号码查询系统

本文档详述了使用哈希表设计电话号码查询系统的课程设计,包括哈希查找技术的应用综述,以及如何用C语言、C++或Python等实现具有人机交互界面的电话簿系统。系统通过电话号码建立哈希表,处理冲突并实现查找与插入功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:利用哈希表设计快速电话号码查询系统
要求:

  1. 通过Internet或知网等工具查找文献,论述哈希表和哈希查找的相关应用综述,字数不少于1500字。
  2. 请你为自己手机的电话簿以电话号码作为关键字建立哈希表,然后依据电话号码进行哈希查找,并采用合适的冲突处理方法处理冲突。查找成功显示姓名与号码,查找失败则进行插入。电话簿初始从文本文档中逐个读入,然后动态生成哈希表。
  3. 采用C语言、C++、python等程序语言实现,要求具有简单的人机交互界面。
  4. 完成课程设计报告,要求涉及问题描述与分析、数据结构与算法设计,代码实现、结果分析等。

数据结构与算法课程设计报告

题目:
姓名:
学号:
班级:

一、 哈希查找技术应用综述(不少于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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值