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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

电话号码本的快速查找

發(fā)布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 电话号码本的快速查找 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

電話號碼本的快速查找

? 創(chuàng)建基于鏈地址法的hash表,并實現(xiàn)電話薄的管理(記錄不超過20個)。
? 電話薄中的記錄包括姓名、電話號碼和地址三個數(shù)據(jù)項,創(chuàng)建兩個hash表,關鍵字分別為姓名和電話號碼
? Hash函數(shù)為姓名前兩個字母/電話號碼最后三個數(shù)字求和之后對17取余
? 完成記錄的插入、查找、顯示功能

輸入0:增加一條記錄。
輸入1:根據(jù)輸入的姓名搜索記錄并輸出。
輸入2:根據(jù)輸入的電話搜索記錄并輸出。
輸入3:根據(jù)姓名查找表輸出全部記錄。
輸入4:根據(jù)電話查找表輸出全部記錄。
輸入5:退出。

#include<iostream> #include<string.h> #define len 17 using namespace std; typedef struct node //create node {char name[8];//define array for name char address[20];//define array for addresschar tel[11];//define array for telephone numnode* next; }Person; typedef struct Hash {Person* data;int count;int sizeindex; }Hash; bool makeH(Hash* table); bool insetH(Hash* nametable, Hash* teltable, Person person); bool searchnameH(Hash* nametable, char name[]); bool searchtelH(Hash* teltable, char tel[]); bool searchname(Person* head, char name[]); bool searchtel(Person* head, char tel[]); int getnamekey(char name[]) {return (name[0] + name[1]) % 17; } int gettelkey(char tel[]) {return((tel[10] - '0') * 10 + (tel[11] - '0')) % 17; } int main() {Hash nametable, teltable;makeH(&nametable);makeH(&teltable);Person *person, *head,*p;head = NULL;int i;char name[8], tel[11];do {cin >> i;switch (i) {case 0:person = new(Person);cin >> person->name >> person->address >> person->tel;person->next = NULL;insetH(&nametable, &teltable, *person);p = head;if (p) {while (p->next) {p = p->next;}p->next = person;}else {head = person;}break;case 1:cin >> name;if (!searchname(head, name))cout << "NULL";break;case 2:cin >> tel;if (!searchtel(head, tel))cout << "NULL";break;case 3:cin >> name;if (!searchnameH(&nametable, name))cout << "NULL";break;case 4:cin >> tel;if (!searchtelH(&teltable, tel))cout << "NULL";break;}} while (i != 5);return 0;delete []nametable.data;delete[]teltable.data;delete person; } bool makeH(Hash* table) {table->data = new Person[len];if (table->data == NULL)return false;int i;Person* p;p = table->data;table->count = 0;table->sizeindex = len;for (i = 0; i < len; i++) {memset(p->name, '\0', sizeof(char));memset(p->tel, '\0', sizeof(char));p++;}return true; } bool insetH(Hash* nametable, Hash* teltable, Person person) {int namekey, nk2, telkey, tk2, c;Person* n, * t,*s;n = nametable->data;t = teltable->data;namekey = getnamekey(person.name);telkey = gettelkey(person.tel);c = 0;int l = strlen((n + namekey)->name);if (l == 0) {memcpy(n + namekey, &person, sizeof(person));nametable->count++;s = n + namekey;}else {nk2 = namekey;do {c++;nk2 = (nk2 + c) % len;} while (strlen((n + namekey)->name) != 0 && c < len);if (c >= len) {cout << "error" << endl;return false;}else {memcpy(n + nk2, &person, sizeof(person));nametable->count++;}}c = 0;l = 0;if (l == 0) {memcpy(t + telkey, &person, sizeof(person));teltable->count++;}else {tk2 = telkey;do {c++;tk2 = (tk2 + c) % len;} while (strlen((t + telkey)->tel) != 0 && c < len);if (c >= len) {cout << "error" << endl;return false;}else {memcpy(t + tk2, &person, sizeof(person));teltable->count++;}}return true; } bool searchnameH(Hash* nametable, char name[]) {int key, c;Person* p;key = getnamekey(name);p = nametable->data;if (strlen((p + key)->name) == 0)return false;else if (strcmp((p + key)->name , name)!=0) {c = 0;do {c++;key = key + c;} while (strcmp((p + key)->name, name) != 0 && c < len);if (c >= len)return false;else {cout << (p + key)->name << ' ' << (p + key)->address << ' ' << (p + key)->tel;return true;}}else {cout << (p + key)->name << ' ' << (p + key)->address << ' ' << (p + key)->tel;return true;}} bool searchtelH(Hash* teltable, char tel[]) {int key, c;Person* p;key = gettelkey(tel);p = teltable->data;if (strlen((p + key)->tel) == 0)return false;else if (strcmp((p + key)->tel , tel)!=0) {c = 0;do {c++;key = key + c;} while (strcmp((p + key)->tel, tel)!=0 && c < len);if (c >= len)return false;else {cout << (p + key)->name << ' ' << (p + key)->address << ' ' << (p + key)->tel;return true;}}else {cout << (p + key)->name << ' ' << (p + key)->address << ' ' << (p + key)->tel;return true;} } bool searchname(Person* head, char name[]) {Person* p;p = head;while (p != NULL && strcmp(p->name , name)!=0) {p = p->next;}if (p == NULL) {return false;}else {cout << p->name << ' ' << p->address << ' ' << p->tel;return true;} } bool searchtel(Person* head, char tel[]) {Person* p;p = head;while (p != NULL && strcmp(p->tel, tel) != 0) {p = p->next;}if (p == NULL) {return false;}else {cout << p->name << ' ' << p->address << ' ' << p->tel;return true;} }

總結

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

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