1 #include <stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<string.h>
5 #include<fstream>
6 # include <conio.h>
7 #define NULL 0
8 unsigned int key;//无符号限制整型
9 unsigned int key1;
10 struct node//建立节点;
11 {
12 int f;
13 char name[8];
14 char address[20];
15 char num[11];
16 node *next;
17 };
18 using namespace std;
19 struct node nam[20];
20 void create(){
21 struct node nam[20];
22 for( int i = 0 ; i < 20 ; i++ ) nam[i].f = 0;
23 }
24 hash(char name[8])
25 {
26 int i=1;
27 int j;
28 key1=(int)name[0];//从第一个字符开始加一直加到最后一个字符;转换成数值
29 while(name[i]!=NULL)
30 {
31 key1+=(int)name[i];
32 i++;
33 }
34 key1=key1%20;//线性探测法,建立一个20长度的表
35 for(j=0;j<20;j++)
36 {
37 key=(key1+j)%20;
38 if(!nam[key].f)//如果插入的这组数据位置在表中是空的,则放入。如果有数据则+1顺延一位;
39 break;
40 }
41 return(key);
42 }
43 node *input(){//输入数据块
44 node *temp;
45 temp=new node;
46 temp->next=NULL;
47 cout<<"输入姓名:"<<endl;
48 cin>>temp->name;
49 cout<<"输入地址:"<<endl;
50 cin>>temp->address;
51 cout<<"输入电话:"<<endl;
52 cin>>temp->num;
53 return temp;
54 }
55 int *apend(){//添加新节点;
56 node *newname;
57 newname=input();
58 newname->next=nam[hash(newname->name)].next;//把新节点插入名字散列表
59 nam[hash(newname->name)].next=newname;
60 return 0;
61 }
62 void list(){//输出显示的是名字的散列表
63 int i;
64 node *p;
65 for(i=0;i<20;i++){
66 p=nam[i].next;
67 while(p){
68 cout<<p->name<<" "<<p->address<<" "<<p->num<<endl;
69 p=p->next;
70 }
71 }
72 }
73 void find(char name[8])//查找名字散列表
74 {
75 int i;
76 int j=0;
77 node *p;
78 for(i=0;i<20;i++){
79 p=nam[i].next;
80 while(p){
81 if(strcmp(name,p->name)==0){//完全匹配
82 cout<<p->name<<" "<<p->address<<" "<<p->num<<endl;
83 j++;
84 }
85 p=p->next;
86 }
87 }
88 if(j==0) cout<<"无此记录"<<endl;
89 }
90 void Delete(char name[8]){//删除名字散列表
91 node *p;
92 hash(name);
93 p=nam[key].next;
94 nam[key].next=p->next;
95 }
96 void menu(){
97 cout<<" 欢迎使用电话号码查找系统 "<<endl;
98 cout<<"******************************************"<<endl;
99 cout<<"1.添加记录"<<" "<<"4.清空记录"<<endl;
100 cout<<"2.查找记录"<<" "<<"5.删除记录"<<endl;
101 cout<<"3.姓名散列"<<" "<<"6.退出系统"<<endl;
102 }
103 int main(){
104 bool quit=false;
105 char num[11];
106 char name[8];
107 create();
108 int sel;
109 menu();
110 cout<<"按任意键开始...";
111 getch();
112 while(!quit){
113 menu();
114 cout<<"请输入你需要执行的命令:";
115 cin>>sel;
116 if(sel!=1||sel!=2||sel!=3||sel!=4||sel!=5||sel!=6||sel!=7){
117 cout<<"注意输入的是非法字符"<<endl;
118 }
119 switch(sel){
120 case 1:
121 cout<<"请输入要添加的内容"<<endl;
122 apend();
123 break;
124 case 2:
125 cout<<"请输入要查寻的名字"<<endl;
126 cin>>name;
127 cout<<"输出查询的内容"<<endl;
128 find(name);
129 break;
130 case 3:
131 cout<<"输出姓名散列表"<<endl;
132 list();
133 break;
134 case 4:
135 cout<<"记录已清空"<<endl;
136 create();
137 break;
138 case 5:
139 cout<<"请输入要删除的名字"<<endl;
140 cin>>name;
141 Delete(name);
142 cout<<"该条记录已删除"<<endl;
143 break;
144 case 6:
145 return 0;
146 }
147 }
148 }