利用哈希表写个电话簿(拉链法解决hash冲突)
1 #include "bits/stdc++.h" 2 #include "conio.h" 3 #include "windows.h" 4 using namespace std; 5 typedef struct node{ 6 char name[100];//姓名 7 char phoneNumber[13];//电话号码 8 struct node * next;//查找下一个的指针 9 }Node; 10 char fname[100] = {'\0'};//输入的联系人名字不得超过100个字符 11 Node People[27];//电话本 12 int hash_lock(char c) 13 { 14 if(c >= 'A' && c <= 'Z') 15 return c - 'A'; 16 else if(c >= 'a' && c <= 'z') 17 return c - 'a'; 18 else return -1; 19 } 20 void Insert()//增加联系人 21 { 22 int key; 23 while(1){ 24 Node *aa = (Node *)malloc(sizeof(Node)); 25 cin >> aa->name; 26 if(aa->name[0] == '-'){ 27 free(aa);//释放空间 28 cout << "Insert over!" << endl; 29 return; 30 } 31 cin >> aa->phoneNumber; 32 if(strlen(aa->phoneNumber) != 11){ 33 cout << "PhoneNumber format is wrong!" << endl; 34 continue; 35 } 36 else { 37 key = hash_lock(aa->name[0]); 38 if(key == -1){ 39 cout << "Name format is wrong!" << endl; 40 cout << "Please input again:" << endl; 41 continue; 42 } 43 } 44 aa->next = People[key].next;//直接头插法 45 People[key].next = aa; 46 cout << "Inserting successful!" << endl; 47 } 48 } 49 void Find(char *findName)//查找联系人 50 { 51 int key; 52 if(findName[0] == '\0' || findName[0] == ' '){ 53 cout << "Find fail!" << endl; 54 return; 55 } 56 key = hash_lock(findName[0]); 57 if(key == -1){ 58 cout << "Your find format is wrong!" << endl; 59 return; 60 } 61 Node *p = People[key].next; 62 while(p){ 63 if(strcmp(p->name,findName) == 0 && strlen(p->name) == strlen(findName)){ 64 cout << "Name:" << p->name << endl; 65 cout << "PhoneNumber:" << p->phoneNumber <<endl; 66 return; 67 } 68 p = p->next; 69 } 70 if(p == NULL){ 71 cout << "Haven't this people" << endl; 72 return; 73 } 74 } 75 void Delete(char *deleteName)//删除联系人 76 { 77 int key;//检索关键字 78 if(deleteName[0] == '\0' || deleteName[0] == ' '){ 79 cout << "Find fail!" << endl; 80 return; 81 } 82 key = hash_lock(deleteName[0]); 83 if(key == -1){//key对应的键值映射到hash表 84 cout << "Your delete format is wrong!" << endl; 85 return; 86 } 87 Node *p1,*p2; 88 p1 = People[key].next; 89 if(p1 == NULL){ 90 cout << "The contact does not exist!" << endl;//如果没找到的话; 91 return; 92 } 93 if(strcmp(p1->name,deleteName) == 0 && strlen(p1->name) == strlen(deleteName)){//防止首字母重名相等 94 People[key].next = p1->next; 95 cout << "Deleting successful!" << endl; 96 free(p1);//释放空间 97 return; 98 } 99 p2 = p1->next; 100 while(p2 != NULL){ 101 if(strcmp(p2->name,deleteName) == 0 && strlen(p2->name) == strlen(deleteName)){//防止首字母重名相等 102 p1 = p2->next; 103 free(p2); 104 cout << "Deleting successful!" << endl; 105 return; 106 } 107 p1 = p2; 108 p2 = p2->next; 109 } 110 cout << "The contact does not exist!" << endl;//如果没找到的话 111 return; 112 } 113 void sFind() 114 { 115 cout << "Input your find name:(input '-' to end)" << endl; 116 while(1){ 117 int i = 0; 118 char c; 119 while((c = getchar()) != '\n')//解决第一个为空格的问题 120 fname[i++] = c; 121 if(i == 0) 122 cout << "Input wrong,try again" << endl; 123 else if(fname[i - 1] == '-'){ 124 cout << "Finding over!" << endl; 125 break; 126 } 127 else 128 Find(fname); 129 memset(fname,0,sizeof(fname));//清空字符串 130 } 131 fflush(stdin);//清空'-'后的回车 132 133 } 134 void sInsert() 135 { 136 cout << "Insert contacts:(input '-' to end)" << endl; 137 Insert();//插入联系人 138 fflush(stdin);//清除'-'后的回车 139 } 140 void sDelete() 141 { 142 cout << "Deleting contacts:(input '-' to end)" << endl; 143 while(1){ 144 int i = 0; 145 char c; 146 while((c = getchar()) != '\n')//解决第一个为空格的问题 147 fname[i++] = c; 148 if(i == 0) 149 cout << "Input wrong,try again" << endl; 150 else if(fname[i - 1] == '-'){ 151 cout << "Deleting over" << endl; 152 break; 153 } 154 else 155 Delete(fname); 156 memset(fname,0,sizeof(fname));//清空字符串 157 } 158 fflush(stdin);//清空'-'后的回车 159 } 160 int main() 161 { 162 int choose; 163 for(int i = 0;i < 27;i++) 164 People[i].next = NULL;//初始化电话簿 165 cout << "****************************Welcome to My phonenumbers!****************************************"<<endl; 166 cout << "****************************You're Phone have three functions**********************************"<<endl; 167 cout << "First:Add contacts(input 1)" << endl;//第一个功能:插入功能 168 cout << "Second:Delete contacts(input 2)" << endl;//第二个功能:删除功能 169 cout << "Third:Find contacts(input 3)" << endl;//第三个功能:查找功能 170 cout << "Declare:If you want to quit,Please input (ESC) key.Thanks for your use!" << endl; 171 while((choose = getch()) != 27){ 172 if((choose - 48) < 1 && (choose - 48) > 3){ 173 cout << "Not have this function,Please input again!" << endl; 174 continue; 175 } 176 switch(choose - 48){ 177 case 1 : sInsert();Sleep(2000);system("cls");break; 178 case 2 : sDelete();Sleep(2000);system("cls");break; 179 case 3 : sFind();Sleep(2000);system("cls");break; 180 } 181 cout << "First:Add contacts(input 1)" << endl;//第一个功能:插入功能 182 cout << "Second:Delete contacts(input 2)" << endl;//第二个功能:删除功能 183 cout << "Third:Find contacts(input 3)" << endl;//第三个功能:查找功能 184 cout << "Declare:If you want to quit,Please input (ESC) key.Thanks for your use!" << endl; 185 } 186 return 0; 187 }
本文来自博客园,作者:{scanner},转载请注明原文链接:{https://home.cnblogs.com/u/scannerkk/}

浙公网安备 33010602011771号