1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 20 #define OK 1 #define FALSE 0
typedef struct{ char name[20]; char phone[20]; char location[30]; }Person;
typedef struct{ Person* array[MAXSIZE]; int count; }HashTable;
int GetKey(char phone[]){ int key=0; int i=0; while(phone[i] != '\0') { key += phone[i]-'0'; i++; }
return key; }
int Hash(int key){ return key%19; }
HashTable* InitHashTable(){ HashTable *hashTable = NULL; hashTable = (HashTable*)malloc(sizeof(HashTable)); hashTable->count = 0; int i; for(i=0;i<MAXSIZE;i++){ hashTable->array[i] = NULL; } }
int Insert(HashTable *hashTable,Person *person){ int key = GetKey(person->phone); int loc = Hash(key); if(hashTable->array[loc] == NULL){ hashTable->array[loc] = person; hashTable->count++; return OK; }else{ int j=1; int k=0; while(1){ int newLoc = -1; if(k%2 == 0){ newLoc = loc + j*j; k++; }else{ newLoc = loc - j*j; j++; k++; if(newLoc < 0){ continue; } } if(newLoc>MAXSIZE){ return FALSE; } if(hashTable->array[newLoc] == NULL){ hashTable->array[newLoc] = person; hashTable->count++; return OK; } } } }
Person* Find(HashTable *hashTable,char phone[]){ int loc = Hash(GetKey(phone)); if(hashTable->array[loc] == NULL){ return NULL; } if(!strcmp(hashTable->array[loc]->phone,phone)){ return hashTable->array[loc]; }else{ int j=1; int k=0; while(1){ int newLoc = -1; if(k%2 == 0){ newLoc = loc + j*j; k++; }else{ newLoc = loc - j*j; j++; k++; if(newLoc < 0){ continue; } } if(newLoc>MAXSIZE || hashTable->array[newLoc] == NULL){ return NULL; } if(!strcmp(hashTable->array[newLoc]->phone,phone)){ return hashTable->array[newLoc]; } } } }
void ShowAll(HashTable *hashTable){ if(hashTable){ int i; printf("一共有%d条记录:\n",hashTable->count); for(i=0;i<MAXSIZE;i++){ if(hashTable->array[i] != NULL){ printf("姓名:%s\t电话:%s\t地址:%s\t\n",hashTable->array[i]->name,hashTable->array[i]->phone,hashTable->array[i]->location); } } printf("加载完毕!"); system("pause"); }else{ printf("请先建立联系簙!"); system("pause"); } }
void menu(){ printf("\n请选择操作"); printf("\n========================================"); printf("\n| 1--建立联系簙 |"); printf("\n| 2--添加联系人 |"); printf("\n| 3--查找联系人 |"); printf("\n| 4--显示所以联系人 |"); printf("\n| 0--退出 |"); printf("\n========================================"); printf("\n请输入菜单号(0-4):"); }
void main(){ HashTable *hashTable = NULL; while(1){ menu(); int i; scanf("%d",&i); if(i == 1){ hashTable = InitHashTable(); printf("建立成功!"); system("pause"); }else if(i == 2){ if(hashTable == NULL){ printf("请先建立联系簙!"); system("pause"); continue; } Person *p = NULL; p = (Person*)malloc(sizeof(Person)); printf("输入电话号码:"); scanf("%s",p->phone); printf("输入姓名:"); scanf("%s",p->name); printf("输入地址:"); scanf("%s",p->location); Insert(hashTable,p); printf("添加成功!\n"); printf("姓名:%s\t电话:%s\t地址:%s\t\n",p->name,p->phone,p->location); system("pause"); } else if(i == 3){ if(hashTable == NULL){ printf("请先建立联系簙!"); system("pause"); continue; } char phone[20]; printf("输入待查找号码:"); scanf("%s",phone); Person *findPerson = Find(hashTable,phone); if(findPerson){ printf("查询成功:\n"); printf("姓名:%s\t电话:%s\t地址:%s\t\n",findPerson->name,findPerson->phone,findPerson->location); system("pause"); }else{ printf("未查询到该联系人!"); system("pause"); } }else if(i == 4){ ShowAll(hashTable); }else if(i == 0){ printf("成功退出系统!"); break; } else{ printf("指令错误!"); system("pause"); } } return; }
|