C語言單鏈表實現(xiàn)通訊錄管理系統(tǒng)
本文實例為大家分享了C語言單鏈表實現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
本人前幾天剛剛自學(xué)了單鏈表,趁熱打鐵,趕緊寫一個小小的項目練練手。
單鏈表的實現(xiàn)在本人之前的博客中有:C語言編寫一個鏈表
通訊錄管理系統(tǒng)
保存人的信息有:
名字 name
電話 telephone
性別 sex
年齡 age
用一個結(jié)構(gòu)體來裝這些信息:
struct infor{ char name[20]; int age; char sex[8]; char telephone[16]; };
實現(xiàn)功能有:
增加聯(lián)系人
刪除聯(lián)系人
修改聯(lián)系人
尋找聯(lián)系人
顯示聯(lián)系人
首先建立鏈表的基本功能創(chuàng)建頭鏈表,創(chuàng)建節(jié)點,插入節(jié)點
struct addre* Creathead(){ //創(chuàng)建頭鏈表 struct addre *headnode = (struct addre*)malloc(sizeof(struct addre)); if (headnode == NULL){ printf("malloc error\n"); } headnode->next = NULL; return headnode; } struct addre* Creatlist(struct infor *list){ //創(chuàng)建節(jié)點 struct addre *node = (struct addre*)malloc(sizeof(struct addre)); if (node == NULL){ printf("malloc error\n"); } node->next = NULL; node->people.age = list->age; strcpy(node->people.name, list->name); strcpy(node->people.sex, list->sex); strcpy(node->people.telephone, list->telephone); return node; } void Addlist(struct addre *headnode,struct infor *list){ //插入節(jié)點 struct addre *t = headnode; while (t->next != NULL){ t = t->next; } struct addre *nodelist = Creatlist(list); t->next = nodelist; nodelist->next = NULL; }
然后在實現(xiàn)通訊錄的功能
void Addpeople(struct addre* node){ //添加人的信息 struct infor *a=malloc(sizeof(struct infor)); // 創(chuàng)建動態(tài)信息結(jié)構(gòu)體指針 if (a == NULL){ printf("malloc error\n"); } printf("請輸入名字\n"); scanf("%s", a->name); printf("請輸入年齡\n"); scanf("%d", &a->age); printf("請輸入性別\n"); scanf("%s", a->sex); printf("請輸入電話號碼\n"); scanf("%s", a->telephone); Addlist(node, a); //用尾插法插入該人信息 printf("添加成功!\n"); } void Deletepeople(struct addre *node){ //刪除人的信息 char *str = malloc(sizeof(char)* 10); if (str == NULL){ //通過名字尋找 printf("malloc error\n"); } printf("請輸入要刪除人的姓名\n"); scanf("%s", str); struct addre *strat = node; struct addre *end = node->next; int flag = 0; //判斷是否找到 0為未找到,1 找到 while (end){ //判斷end的 不然會越界 if (strcmp(end->people.name, str) == 0){ flag = 1; break; } node = node->next; //到下一個鏈表 strat = node; //一個指向前面 一個指向后面,刪除將end刪除,前面那個直接指向end的指向 end = node->next; } if (flag){ strat->next = end->next; printf("刪除成功\n"); free(end); } else{ printf("沒找到!\n"); } } void Modifyinfor(struct addre *node){ //修改人的信息 char *str = malloc(sizeof(char)* 10); //通過名字尋找 if (str == NULL){ printf("malloc error\n"); } printf("請輸入要修改人的姓名\n"); scanf("%s", str); int flag = 0; while (node){ if (strcmp(node->people.name, str) == 0){ flag = 1; break; } node = node->next; } if (flag){ printf("請重新輸入該人信息\n"); printf("請輸入名字\n"); scanf("%s", node->people.name); printf("請輸入年齡\n"); scanf("%d", &node->people.age); printf("請輸入性別\n"); scanf("%s", node->people.sex); printf("請輸入電話號碼\n"); scanf("%s", node->people.telephone); printf("修改成功\n"); } else{ printf("沒找到\n"); } } void Foundpeople(struct addre *node){ //找到某人的信息并打印出來 char *str = malloc(sizeof(char)* 10); //通過名字尋找 if (str == NULL){ printf("malloc error\n"); } printf("請輸入要查找人的姓名\n"); scanf("%s", str); int flag = 0; while (node){ if (strcmp(node->people.name, str) == 0){ flag = 1; break; } node = node->next; } if (flag){ printf("name\tage\tsex\ttelephone\n"); printf("%s\t", node->people.name); printf("%d\t", node->people.age); printf("%s\t", node->people.sex); printf("%s\t", node->people.telephone); } else{ printf("沒找到!\n"); } } void Display(struct addre *node){ struct addre *pmove = node->next; //要從頭節(jié)點的下一個節(jié)點顯示信息 printf("name\tage\tsex\ttelephone\n"); while (pmove){ printf("%s\t%d\t%s\t%s\n", pmove->people.name, pmove->people.age, pmove->people.sex, pmove->people.telephone); pmove = pmove->next; } }
其它代碼
菜單:
void Menu(){ printf("+-----------------+\n"); printf("+-1.add 2.delet-+\n"); printf("+-3.modify 4.seek-+\n"); printf("+-5.display6.exit-+\n"); printf("+-----------------+\n"); printf("Please Enter Your Select\n"); }
本人使用的時多文件的方式上面的代碼都在函數(shù)定義的源文件里實現(xiàn)
main函數(shù)的源文件代碼:
#include"addressbank.h" /*********************** 信息: 名字 name 年齡 age 電話 telephone 性別 sex 功能: 增加 刪除 修改 尋找 打印 顯示 ***********************/ int main(){ struct addre* node = Creathead(); int quit = 0; while (!quit){ Menu(); int select = 0; scanf("%d", &select); switch (select){ case 1: Addpeople(node); break; case 2: Deletepeople(node); break; case 3: Modifyinfor(node); break; case 4: Foundpeople(node); break; case 5: Display(node); break; case 6: quit = 1; break; default: printf("Enter Error!\n"); break; } } system("pause"); return 0; }
聲明的頭文件:
#ifndef __ADDRESSBANK_H__ #define __ADDRESSBANK_H__ #include<stdio.h> #include<string.h> struct infor{//信息結(jié)構(gòu)體 char name[20]; int age; char sex[8]; char telephone[16]; }; struct addre{ //鏈表 struct infor people; struct addre *next; }; //功能函數(shù) extern void Menu(); extern void Addpeople(struct addre *node); extern void Deletepeople(struct addre *node); extern void Modifyinfor(struct addre *node); extern void Foundpeople(struct addre *node); extern void Display(struct addre *node); //下面未鏈表函數(shù) extern struct addre* Creatlist(struct infor *list); extern struct addre* Creathead(); extern void Addlist(struct addre *headnode, struct infor *list); #endif
代碼可直接復(fù)制使用,如有不足請?zhí)岢?,后續(xù)修改謝謝
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++?Qt實現(xiàn)文章小說人物關(guān)系分析
這篇文章主要為大家詳細(xì)介紹了C/C++?Qt如何實現(xiàn)文章小說人物關(guān)系分析功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01