当前位置:首页 > 通信资讯 > 正文

本文实例为大家分享了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 #include<stdio.h> #include<stdlib.h> #include<string.h> #pragma warning(disable:4996);//解决VS报严重性代码错误 typedef struct LNode { char name[20]; double ph_number; struct LNode* next; }LinkNode; //创建通讯录 LNode* CreateList(LNode*& L) { LNode* s, * r; int n; L = (LNode*)malloc(sizeof(LNode)); r = L; printf("请输入要创建联系人的个数:"); scanf("%d", &n); printf("\n"); for (int i = 0; i < n; i++) { s = (LNode*)malloc(sizeof(LNode)); printf("请输入第%d个联系人的姓名,电话:\n", i + 1); scanf("%s%lf", &s->name, &s->ph_number); printf("\n"); r->next = s; r = s; } r->next = NULL; return L; } //添加联系人 void ListInsert(LNode*& L) { LNode* new_s, * r = L; while (r->next != NULL) { r = r->next; } new_s = (LNode*)malloc(sizeof(LNode)); printf("请输入要添加的联系人的姓名,电话:\n"); scanf("%s%lf", &new_s->name, &new_s->ph_number); printf("\n"); r->next = new_s; r = new_s; r->next = NULL; } //查找联系人 bool Locate(LNode*& L) { LNode* p = L->next; char name_[20]; printf("请输入要查找的联系人的姓名:\n"); scanf("%s", &name_); printf("\n"); while (p != NULL && strcmp(p->name, name_) != 0)//注意判断条件 { p = p->next; } if (p == NULL) return false; else return true; } //修改联系人 bool ModifyList(LNode*& L) { LNode* p = L; char name_[20]; double ph_number_; printf("请输入要修改的联系人的姓名:\n"); scanf("%s", name_); printf("\n"); while (p != NULL && strcmp(p->name, name_) != 0)//注意判断条件 { p = p->next; } if (p == NULL) return false; else { printf("请输入修改后的电话号码为:\n"); scanf("%lf", &ph_number_); printf("\n"); p->ph_number = ph_number_; return true; } } //删除联系人 bool ListDelete(LNode*& L) { LNode* p = L->next, * q = L; char name_[20]; printf("请输入要删除联系人的姓名:\n"); scanf("%s", name_); printf("\n"); while (p != NULL && strcmp(p->name, name_) != 0)//注意判断条件 { p = p->next; q = q->next; } if (p == NULL) return false; else { q->next = q->next->next; free(p); return true; } } //加载通讯录 void DispList(LNode* L) { int i = 0; LNode* p = L->next; while (p != NULL) { printf("姓名:%s 电话:%.0lf\n", p->name, p->ph_number); p = p->next; i++; } } int main() { printf("*****************************************\n"); printf("* *\n"); printf("* 1:添加联系人 *\n"); printf("* *\n"); printf("* 2: 查找联系人 *\n"); printf("* *\n"); printf("* 3: 修改联系人 *\n"); printf("* *\n"); printf("* 4: 删除联系人 *\n"); printf("* *\n"); printf("* 5: 加载通讯录 *\n"); printf("* *\n"); printf("*****************************************\n"); printf("\n"); LNode* L = CreateList(L); int operand; printf("\n"); for (int i = 0; i < 5; i++) { printf("请输入您要执行操作的操作数:\n"); scanf("%d", &operand); switch (operand) { case 1: ListInsert(L); break; case 2: if (Locate(L) == 1) { printf("找到该联系人\n"); printf("\n"); break; } else { printf("未找到改联系人\n"); printf("\n"); break; } case 3: if (ModifyList(L) == 1) { printf("修改成功\n"); printf("\n"); break; } else { printf("修改失败\n"); printf("\n"); break; } case 4: if (ListDelete(L) == 1) { printf("删除成功\n"); printf("\n"); break; } else { printf("删除失败\n"); printf("\n"); break; } case 5: DispList(L); break; default: printf("ERROR!!!\n"); } } return 0; }

运行截图

单链表通讯录 用c语言实现(用链表通讯录c语言程序设计)

单链表通讯录 用c语言实现(用链表通讯录c语言程序设计)

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

原文链接:https://blog.csdn.net/qq_43757941/article/details/105725509

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。