日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

哈希表实现电话号码查找系统

發布時間:2023/12/20 windows 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈希表实现电话号码查找系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計任務

設計哈希表實現電話號碼查找系統

  • 設每個記錄有下列數據項:電話號碼、用戶名、地址
  • 從鍵盤輸入個記錄,分別以電話號碼和用戶名為關鍵字建立不同的哈希表
  • 采用線性探測再散列的方法解決沖突
  • 查找并顯示給定電話號碼的記錄
  • 查找并顯示給定用戶名的記錄
  • 主要算法功能

    主要4個功能:

    • 創建鏈表
    • 查詢(通過名字/電話)
    • 顯示
    • 退出

    以下是簡易流程圖:

    代碼

    為了讓主代碼main.c看起來更加清晰,我把功能函數集成在一個頭文件中hash.h。

    代碼里用不大好的英文注釋,能懂9行

    main.c文件


    #include"hash.h"int main() {char s[20];int n,Fn;int num;//contact numberBridge head;//include namehash table and phonehash tablewhile(1){Menu();printf("Enter:");scanf("%d",&n);switch(n){case 1:printf("Enter number of contacts:");scanf("%d",&num);Fn=Fprime(num);//get next primehead=Create(num);//creat linkClean();break;case 2:Query(head,Fn);Clean();break;case 3:ShowAll(head,Fn);//print all contactsClean();break;case 4:printf("\nThank you for using my software!\n");return 0;}} }

    hash.h文件


    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define MAXNUMBER 100000 typedef struct node {char name[8];char phone[12];char adress[50];int state;struct node *next; }contnode; typedef struct {contnode *Hn;//head node of namecontnode *Hp;//head node of phone }Bridge; void Menu() {printf("\n----------Menu----------\n\n");printf(" 1.Create contacts\n");printf(" 2.Query contacts\n");printf(" 3.Show all contacts\n");printf(" 4.Exit\n");printf("------------------------\n");} void Clean()//clear window {printf("\nClear window? (Y/N)\t");getchar();//filter \nif(getchar()=='Y')system("cls"); } void ShowAll(Bridge h,int n)//show all contacts {contnode *p=h.Hp;printf("\nName\tPhone\t\tAdress");printf("\n--------------------------------------\n");while(n--){if(p->state==1)printf("%s\t%s\t%s\n",p->name,p->phone,p->adress); p=p->next;} } int Fprime(int n)//find next prime {int i,p=(n%2)?n+2:n+1;while(p<=MAXNUMBER){for(i=(int)sqrt(p);i>2;i--)if(!(p%i))break;if(i==2)break;else p+=2;} return p; } contnode* CreatTable(int num)//insert tail & init {contnode *p,*q,*h;q=p=h=(contnode*)malloc(sizeof(contnode));h->state=0;while(num--){p=(contnode*)malloc(sizeof(contnode));strcpy(p->name,"\0");strcpy(p->adress,"\0");strcpy(p->phone,"\0");p->state=0;q->next=p;q=p;}p->next=NULL;free(p);free(q);return h; } int Ghashn(char *key,int num)//get hash name number {unsigned int h=0;while(*key!='\0')h=(h<<5)+*key++;return h%num; } int Ghashp(char *key,int num)//get hash phone number {int i;int sum=0;for(i=6;i<=10;i++)sum=sum*10+key[i]-'0';return sum%num; }void HashTable(char *name,char *phone,char *adress,contnode *h,int flag,int size)//create table {int seat;contnode *p=h;if(flag)seat=Ghashp(phone,size);elseseat=Ghashn(name,size);while(seat&&seat--)//Go to the designated locationp=p->next;while(p->state!=0)//Linear Probing(if state==1 ,find the next one){if(p->next==NULL){p=h;continue; } p=p->next;} strcpy(p->name,name);strcpy(p->adress,adress);strcpy(p->phone,phone); p->state=1; } void show(contnode *n)//print the contact {printf("\nName\tPhone\t\tAdress");printf("\n--------------------------------------\n");printf("%s\t%s\t%s\n",n->name,n->phone,n->adress); }void FindSeat(contnode *h,int size,int flag)//find seat of number {char key[15];int seat,bseat,f=1;contnode *p=h;printf("\nEnter key:");scanf("%s",key);if(flag)seat=Ghashp(key,size);//find table number of phoneelse seat=Ghashn(key,size);//find table number of namebseat=seat;while(seat>0&&--seat)p=p->next;if(flag)while(strcmp(p->phone,key)!=0){if(p->next==NULL)//If to the end back to the head{f=1;p=h;}if(f)bseat++;if(bseat>=size)//If steps are larger than size return{printf("\nSorry,not found!\n");return;}p=p->next;}elsewhile(strcmp(p->name,key)!=0){if(p->next==NULL){f=1;p=h;}if(f)bseat++;if(bseat>=size){printf("\nSorry,not found!\n");return;}p=p->next;}show(p);//print node } Bridge Create(int num) {int i;contnode *Hp,*Hn;Bridge h;char name[8],adress[20],phone[12];Hp=CreatTable(Fprime(num));//get head nodeHn=CreatTable(Fprime(num));for(i=0;i<num;i++){printf("\n%dst contact:",i+1);printf("\nEnter name:");scanf("%s",name);printf("Enter phone:");scanf("%s",phone);printf("Enter adress:");scanf("%s",adress);HashTable(name,phone,adress,Hn,0,Fprime(num));//create hashtable by nameHashTable(name,phone,adress,Hp,1,Fprime(num));//create hashtable by phone}h.Hn=Hn;h.Hp=Hp;return h; } void Query(Bridge head,int size) {int n;printf("\n-------Choose way-------\n");printf("1.By Name\n");printf("2.By Phone\n"); printf("------------------------\n");printf("Enter:"); scanf("%d",&n);switch(n){case 1:FindSeat(head.Hn,size,0);break;case 2:FindSeat(head.Hp,size,1);break;} }

    總結

    以上是生活随笔為你收集整理的哈希表实现电话号码查找系统的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。