本文实例为大家分享了C++实现链表版本通讯录的具体代码,供大家参考,具体内容如下
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
#include <iostream> #include <string> using namespace std; class Address; class Contact{ private : string name; string sex; string tel; string QQ; string address; string addition; Contact *next; public : Contact(); friend class Address; }; Contact::Contact() { next = NULL; } class Address{ public : Address(); ~Address(); int show(); void insert(); void delete_per(); void display(); void search(); void update(); private : Contact *head; }; Address::Address() { head = new Contact; if (head == NULL) { cout<< "fail create" <<endl; } } Address::~Address() { delete head; } int Address::show() //主菜单函数 { int choice = 0 ; cout<< "\t\t\t\t**************************" <<endl; cout<< "\t\t\t\t* 通讯录c++简易版本 *" <<endl; cout<< "\t\t\t\t**************************" <<endl; cout<< "\t\t\t\t* 1、添加 2、删除 *" <<endl; cout<< "\t\t\t\t**************************" <<endl; cout<< "\t\t\t\t* 3、查看 4、搜索 *" <<endl; cout<< "\t\t\t\t**************************" <<endl; cout<< "\t\t\t\t* 5、更新 6、退出 *" <<endl; cout<< "\t\t\t\t**************************" <<endl; cout<< "\t\t\t\t请输入选择:" ; cin>>choice; while (!(choice >= 1&&choice <= 6)) { while ( getchar ()!= '\n' ); cout<< "输入有误,请重新输入!" ; cin>>choice; } return choice; } void Address::insert() //添加联系人 { Contact *p = head; char relay = 0; while (p->next != NULL) { p = p->next; } Contact *person = new Contact; cout<< "请输入姓名:" ; cin>>person->name; cout<< "请输入性别:" ; cin>>person->sex; cout<< "请输入电话:" ; cin>>person->tel; cout<< "请输入QQ:" ; cin>>person->QQ; cout<< "请输入住址:" ; cin>>person->address; cout<< "请输入备注:" ; cin>>person->addition; p->next = person; person->next = NULL; cout<< "\n添加成功,是否继续添加?(y/n)" ; cin>>relay; while (!(relay == 'y' ||relay == 'Y' ||relay == 'N' ||relay == 'n' )) { cout<< "输入错误,请重新输入(y/n):" ; cin>>relay; } if (relay == 'y' ||relay == 'y' ) { system ( "clear" ); insert(); } } void Address::delete_per() //删除联系人 { string m_name; Contact *p = head; Contact *pre = head; int flag = 0; cout<< "请输入你要删除的联系人姓名!" ; cin>>m_name; while (p->next != NULL) { pre = p; p = p->next; if (p->name == m_name) { pre->next = p->next; delete p; p = NULL; flag = 1; break ; } } if (flag == 1) { cout<< "删除成功!" <<endl; } else { cout<< "您删除的联系人不存在,删除失败!" <<endl; } } void Address::display() //查看联系人 { Contact *p = head; while (p->next != NULL) { p = p->next; cout<<endl<< "=======================================" <<endl; cout<< "姓名:" <<p->name<<endl; cout<< "性别:" <<p->sex<<endl; cout<< "电话:" <<p->tel<<endl; cout<< "QQ:" <<p->QQ<<endl; cout<< "地址:" <<p->address<<endl; cout<< "备注:" <<p->addition<<endl; } } void Address::search() //搜索联系人 { string m_name; Contact *p = head; int flag = 0; cout<< "请输入你要搜索的联系人姓名:" ; cin>>m_name; while (p->next != NULL) { p = p->next; if (p->name == m_name) { cout<<endl<< "=======================================" <<endl; cout<< "姓名:" <<p->name<<endl; cout<< "性别:" <<p->sex<<endl; cout<< "电话:" <<p->tel<<endl; cout<< "QQ:" <<p->QQ<<endl; cout<< "地址:" <<p->address<<endl; cout<< "备注:" <<p->addition<<endl; flag = 1; } } if (flag == 1) { cout<< "\n查询成功!" <<endl; } else { cout<< "您查询的联系人不存在,删除失败!" <<endl; } } void Address::update() //修改联系人 { Contact *p = head; string m_name; int flag = 0; cout<< "请输入你要更新的姓名:" ; cin>>m_name; while (p->next != NULL) { p = p->next; if (p->name == m_name) { cout<< "请更新性别:" ; cin>>p->sex; cout<< "请更新电话:" ; cin>>p->tel; cout<< "请更新QQ:" ; cin>>p->QQ; cout<< "请更新住址:" ; cin>>p->address; cout<< "请更新备注:" ; cin>>p->addition; flag = 1; break ; } } if (flag == 1) { cout<< "\n更新成功" <<endl; } else { cout<< "查无此人,更新失败!" <<endl; } } int main() { Address *person = new Address; int choice = 0; while (1) { system ( "clear" ); choice = person->show(); switch (choice) { case 1: { system ( "clear" ); person->insert(); break ; } case 2: { system ( "clear" ); person->delete_per(); break ; } case 3: { system ( "clear" ); person->display(); break ; } case 4: { system ( "clear" ); person->search(); break ; } case 5: { system ( "clear" ); person->update(); break ; } case 6: { exit (0); } } cout<< "\n\n按任意键返回....." ; getchar (); getchar (); } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_38076413/article/details/75093105