服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++使用链表存储实现通讯录功能管理

C++使用链表存储实现通讯录功能管理

2023-02-07 14:55乐公 C/C++

这篇文章主要为大家详细介绍了C++使用链表存储实现通讯录功能管理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++使用链表存储实现通讯录功能管理的具体代码,供大家参考,具体内容如下

简介

这是第二周老师给的一个小项目要求实现基本通讯录功能,有数据的增删改查,包含插入时间的能力。

代码详情

头文件

?
1
2
3
4
5
#include <iostream>
#include <string>
#include<malloc.h> //system功能调用 
#include <windows.h> //使用本地系统API获取插入时间 
#include <sstream>

基本存储结构体

?
1
2
3
4
5
6
7
8
9
10
11
typedef struct info{
    string number;
    string date;
    string name;
    string adress;
    string birthday; 
}A;
typedef struct LNode{
    A data; 
    struct LNode *next;
}LNode,*LinkList;

链表数据初始化
用前插法插入数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int InitList(LinkList &L){ //初始化链表 
    L = new LNode;
    L->next = NULL;
    return OK;
}
int ListInsert(LinkList &L,string name,string adress,string birthday,string date,string number){ //考虑使用数组来赋值 
    LinkList p; p= new LNode;
    p->data.name = name;
    p->data.adress = adress;
    p->data.date = date;
    p->data.birthday = birthday;
    p->data.number = number;
    p->next = L->next;
    L->next = p;
    return OK;

本地WindowsAPI调用插入时间

?
1
2
3
4
5
6
SYSTEMTIME sys; 
GetLocalTime( &sys );
string y = doubleToString(sys.wYear);
string m = doubleToString(sys.wMonth);
string d = doubleToString(sys.wDay);
string ymd = y+"-"+m+"-"+d;

因为获取的是一个double值,您得对其时间强制类型转换

?
1
2
3
4
5
6
7
8
string doubleToString(double num)
{ //强制类型转换 double强制转换为string类型 
    stringstream ss;
    string str;
    ss << num;
    ss >> str;
    return str;
}

数据查询功能

?
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
LinkList SearchElemChar(LinkList L,int i,string e){ //思路,传递参数1,2,3,4,eg 1代表name,再分不同的方法在链表内循环查找操作 
 if (i == 1){ //查名字 
     while(L!= NULL){
         if(L->data.name == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
             cout << "未查到数据!"<<endl;
         }
 }else if(i == 2){//查生日 
     while(L!= NULL){
         if(L->data.birthday == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 else if(i == 3){//查地址 
     while(L!= NULL){
         if(L->data.adress == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }else if(i == 4){//查时间 
     while(L!= NULL){
         if(L->data.date == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 else if(i == 5){//查电话 
     while(L!= NULL){
         if(L->data.number == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 

完整案例

?
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//乐公第二周项目 实现基本通讯录存储结构 
//基础链表存储数据
#include <iostream>
#include <string>
#include<malloc.h> //system功能调用 
#include <windows.h> //使用本地系统API获取插入时间 
#include <sstream>
#define ERROR 0
#define OK 1
using namespace std;
typedef  int ElemType; /*定义表元素的类型*/
//结构体文件
typedef struct info{
    string number;
    string date;
    string name;
    string adress;
    string birthday; 
}A;
typedef struct LNode{
    A data; 
    struct LNode *next;
}LNode,*LinkList;
 
int InitList(LinkList &L){ //初始化链表 
    L = new LNode;
    L->next = NULL;
    return OK;
}
int ListInsert(LinkList &L,string name,string adress,string birthday,string date,string number){ //考虑使用数组来赋值 
    LinkList p; p= new LNode;
    p->data.name = name;
    p->data.adress = adress;
    p->data.date = date;
    p->data.birthday = birthday;
    p->data.number = number;
    p->next = L->next;
    L->next = p;
    return OK;
//查找元素 (难题需要解决)
 
LinkList SearchElemChar(LinkList L,int i,string e){ //思路,传递参数1,2,3,4,eg 1代表name,再分不同的方法在链表内循环查找操作 
 if (i == 1){ //查名字 
     while(L!= NULL){
         if(L->data.name == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
             cout << "未查到数据!"<<endl;
         }
 }else if(i == 2){//查生日 
     while(L!= NULL){
         if(L->data.birthday == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 else if(i == 3){//查地址 
     while(L!= NULL){
         if(L->data.adress == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }else if(i == 4){//查时间 
     while(L!= NULL){
         if(L->data.date == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 else if(i == 5){//查电话 
     while(L!= NULL){
         if(L->data.number == e){
             return L; 
         }else{
         L = L->next;
         }
         }
         if(L = NULL){
         cout <<"未查到数据!"<<endl;
         }
 }
 
 
LinkList SearchElemBefore(LinkList L,string e){ //删除结点必须返回前个结点的地址,这里查找前个结点 
     LinkList P; 
     while(L!= NULL){
         if(L->data.name == e){
             return P; 
         }else{
         P = L;
         L = L->next;
         }
         }
         if(L = NULL){
             return L; 
         }
int ShowMenu(){ //主菜单 
    int a;
    cout<<"------欢迎您使用乐公通讯录系统!------"<< endl;
    cout <<"-----请根据功能输入对应的序号--------" <<endl; 
    cout <<"-----------1.新建联系人--------------" <<endl; 
    cout <<"--------2.查看所有联系人-------------" <<endl; 
    cout <<"--------3.修改选定联系人-------------" <<endl;
    cout <<"--------4.查询选定联系人-------------" <<endl;
    cout <<"--------5.删除选定联系人-------------" <<endl;
    cout <<"------------6.退出系统---------------" <<endl; 
    cout << "请您输入序号并按回车进入:"
    cin >> a;
    return a;
}
 
string doubleToString(double num)
{ //强制类型转换 double强制转换为string类型 
    stringstream ss;
    string str;
    ss << num;
    ss >> str;
    return str;
}
void foreachelem(LinkList L){
    if(L->next == NULL){
        cout<<"通讯录里还没有联系人,快去新建一下吧~"<<endl; 
    }else{
    while(L->next!=NULL){
        L=L->next;
        cout<< "联系人姓名:" << L->data.name <<endl;
        cout<< "联系人电话:" << L->data.number<<endl;
        cout<< "联系人地址:" << L->data.adress <<endl;
        cout<< "联系人生日:" << L->data.birthday <<endl;
        cout<< "录入时间:" << L->data.date <<endl;
    }
    cout<< "\n";
}
//system("pause");
}
int serachsamename(LinkList L,string name){ //查找是否存在姓名相同的联系人 
    while(L->next!=NULL){
        L=L->next;
        if(L->data.name==name)return 0;
    }
    return 1;
int main(){
    int i;
    LinkList L;
    InitList(L);
    while(i!=6){
        i = ShowMenu();
        if(i ==1){
            cout << "您选择了:新建联系人" <<endl; 
            string number;
            string date;
            string time;
            string name;
            string adress;
            string birthday; 
            cout << "请输入联系人姓名:"
            cin >> name;
             SYSTEMTIME sys; 
             GetLocalTime( &sys );
             string y = doubleToString(sys.wYear);
             string m = doubleToString(sys.wMonth);
             string d = doubleToString(sys.wDay);
             string ymd = y+"-"+m+"-"+d;
            int repeat = serachsamename(L,name); 
            if(repeat == 0){
                cout << "联系人姓名重复,请删除旧联系人或更改姓名!" << endl; 
             }else{
                 cout << "请输入联系人电话:"
                cin >> number;
            if(number.size()!=11){
                cout << "手机号输入有误,请大于11位!" << endl;    
            }else{
                cout << "请输入联系人生日:"
            cin >> birthday;
            cout << "请输入联系人地址:"
            cin >> adress;
                cout << "联系人于" << ymd;
                int ok;
            ok =  ListInsert(L,name,adress,birthday,ymd,number);
            if(ok == 1){
                cout << "日新建成功!" << endl; 
            }else
            cout << "新建失败!" << endl;
            
            }
        }
            system("pause");
            system("cls");
        }else if(i==2){
            cout << "您选择了:遍历联系人" <<endl; 
            foreachelem(L);
            system("pause");
            system("cls");
            
        }else if(i==3){
            cout << "您选择了:修改选定联系人" <<endl; 
            cout <<"请输入要修改的联系人姓名:" ;
            string name;
            cin >> name;
            LinkList B;
            B = SearchElemChar(L,1,name);
             if(B){
                 system("cls");
                 cout << "联系人查找成功!姓名:" << B->data.name << endl; 
                 int select;
                 cout <<"---------修改姓名请输入1---------" << endl;
                cout <<"---------修改电话请输入2---------" << endl; 
                cout <<"---------修改生日请输入3---------" << endl; 
                cout <<"---------修改地址请输入4---------" << endl; 
                 cout <<"请根据序号输入对象的选项修改:" ;
                 cin >> select;
                 switch(select){
                     case 1: 
                     {    string name;
                    cout <<"请输入新姓名:"
                    cin >> name;
                    B->data.name = name;
                    cout <<"修改完成!" << endl;
                    break
                     }
                    case 2: {
                    string number;
                    cout <<"请输入新电话:"
                    cin >> number;
                    if(number.size()!=11){
                    cout << "手机号输入有误,请大于11位!" << endl;    
                    }else{
                    B->data.number = number;
                    cout <<"修改完成!" << endl;
                    }
                    break;
                    }
                     case 3:{
                         string birthday;
                         cout <<"请输入新生日:"
                         cin >> birthday;
                         B->data.birthday = birthday;
                        cout <<"修改完成!" << endl;
                        break;
                     }
                     case 4:{
                         string adress;
                         cout <<"请输入新地址:"
                         cin >> adress;
                         B->data.adress = adress;
                        cout <<"修改完成!" << endl;
                        break;
                     }
                     default:cout <<"序号输入错误,请重新输入!"<<endl; 
                 }
             }else{
                 cout << "未查找到联系人!请重新输入!" << endl; 
             }
              
            system("pause");
            system("cls");
        }else if(i==4){
            system("cls");
            cout << "您选择了:查询选定联系人" <<endl; 
                cout <<"---------根据姓名查询请输入1---------" << endl;
                cout <<"---------根据电话查询请输入2---------" << endl; 
                cout <<"---------根据生日查询请输入3---------" << endl; 
                cout <<"---------根据地址查询请输入4---------" << endl;
                int select;
                cout <<"请根据序号输入对象的进行查询:" ;
                 cin >> select;
                 switch(select){
                     case 1:{
                         cout <<"请输入要查询的联系人姓名:" ;
                        string name;
                        cin >> name;
                        LinkList B;
                        B = SearchElemChar(L,1,name);
                        if(B){
                            cout<<"查询成功!"<< endl; 
                            cout<<"联系人姓名:"<< B->data.name << endl;
                            cout<<"联系人电话:"<< B->data.number << endl;
                            cout<<"联系人生日:"<< B->data.birthday << endl;
                            cout<<"联系人地址:"<< B->data.adress << endl;
                            cout<<"插入日期:"<< B->data.date << endl;
                         }else{
                             cout<<"查询失败!请重新输入!"<< endl; 
                         }
                        break;
                     }
                     case 2:
                         {
                         cout <<"请输入要查询的联系人电话:" ;
                        string number;
                        cin >> number;
                        LinkList B;
                        B = SearchElemChar(L,5,number);
                        if(B){
                            cout<<"查询成功!"<< endl; 
                            cout<<"联系人姓名:"<< B->data.name << endl;
                            cout<<"联系人电话:"<< B->data.number << endl;
                            cout<<"联系人生日:"<< B->data.birthday << endl;
                            cout<<"联系人地址:"<< B->data.adress << endl;
                            cout<<"插入日期:"<< B->data.date << endl;
                         }else{
                             cout<<"查询失败!请重新输入!"<< endl; 
                         }
                        break;
                         }
                        case 3:{
                            cout <<"请输入要查询的联系人生日:" ;
                        string bd;
                        cin >> bd;
                        LinkList B;
                        B = SearchElemChar(L,2,bd);
                        if(B){
                            cout<<"查询成功!"<< endl; 
                            cout<<"联系人姓名:"<< B->data.name << endl;
                            cout<<"联系人电话:"<< B->data.number << endl;
                            cout<<"联系人生日:"<< B->data.birthday << endl;
                            cout<<"联系人地址:"<< B->data.adress << endl;
                            cout<<"插入日期:"<< B->data.date << endl;
                         }else{
                             cout<<"查询失败!请重新输入!"<< endl; 
                         }
                        break;
                        }
                        case 4:{
                        cout <<"请输入要查询的联系人地址:" ;
                        string ad;
                        cin >> ad;
                        LinkList B;
                        B = SearchElemChar(L,3,ad);
                        if(B){
                            cout<<"查询成功!"<< endl; 
                            cout<<"联系人姓名:"<< B->data.name << endl;
                            cout<<"联系人电话:"<< B->data.number << endl;
                            cout<<"联系人生日:"<< B->data.birthday << endl;
                            cout<<"联系人地址:"<< B->data.adress << endl;
                            cout<<"插入日期:"<< B->data.date << endl;
                         }else{
                             cout<<"查询失败!请重新输入!"<< endl; 
                         }
                        break;
                            break;
                        }
                     default:cout <<"序号输入错误,请重新输入!"<<endl; 
                
            system("pause");
            system("cls");
        }else if(i==5){
            cout << "您选择了:删除联系人" <<endl; 
            string name;
            cout << "请输入联系人姓名:" <<endl; 
            cin >> name;
            LinkList D,P;
            P = SearchElemBefore(L,name); 
            if(P){
                D = P->next;
            P->next = D->next;
            delete D; //释放结点数据 
            cout << "删除成功!" <<endl;
            }else{
                cout<<"查询失败!未找到指定联系人!"<< endl; 
            }
            system("pause");
            system("cls");
        }else if(i==6){
            cout << "系统已退出,欢迎下次使用!" <<endl;
        }
        else{
            cout <<"输入异常,请重新选择输入!" <<endl;
            system("pause");
            system("cls");
        }
    
    return 0;
}

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/jzfx12211/article/details/109741291

延伸 · 阅读

精彩推荐
  • C/C++详解约瑟夫环问题及其相关的C语言算法实现

    详解约瑟夫环问题及其相关的C语言算法实现

    这篇文章主要介绍了详解约瑟夫环问题及其相关的C语言算法实现,也是ACM当中经常会引用到的基础题目,文中共介绍了三种C语言解答,需要的朋友可以参考下...

    低调小一4002021-03-06
  • C/C++C/C++实现线性单链表的示例代码

    C/C++实现线性单链表的示例代码

    使用链存储结构的线性存储结构为线性单链表,本文将分别利用C语言和C++实现线性单链表,感兴趣的小伙伴可以跟随小编一起学习一下...

    学编程的闹钟3532022-12-09
  • C/C++C语言实题讲解快速掌握单链表下

    C语言实题讲解快速掌握单链表下

    单链表是后面要学的双链表以及循环链表的基础,要想继续深入了解数据结构以及C语言,我们就要奠定好这块基石!接下来就和我一起学习吧...

    三分苦8242022-11-04
  • C/C++C++深入讲解namespace与string关键字的使用

    C++深入讲解namespace与string关键字的使用

    namespace命名空间或者叫名字空间,传统的c++只有一个全局的namespace,namespace引入了复杂性。namespace允许像类,对象,函数聚集在一个名字下。本质上讲nam...

    Mi ronin9712022-12-07
  • C/C++c++ 中vector 常见用法

    c++ 中vector 常见用法

    这篇文章主要给大家分享的是c++ 中vector 常见用法,,vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capacity,表示当前vector在...

    诗子黎3462022-02-28
  • C/C++哈夫曼编码算法构造代码

    哈夫曼编码算法构造代码

    这篇文章主要介绍了哈夫曼编码算法构造代码,有需要的朋友可以参考一下...

    C语言教程网11492021-01-12
  • C/C++C语言详解结构体的内存对齐与大小计算

    C语言详解结构体的内存对齐与大小计算

    C 数组允许定义可存储相同类型数据项的变量,结构是 C 编程中另一种用户自定义的可用的数据类型,它允许你存储不同类型的数据项,本篇让我们来了解...

    CodeWinter6672022-11-15
  • C/C++C++面向对象实现万年历的示例代码

    C++面向对象实现万年历的示例代码

    本文将通过面向对象实现一个简单的日历(万年历)效果,主要会有以下几个模块:模型、视图、控制,感兴趣的小伙伴可以动手尝试一下...

    Mi ronin6522022-12-16