您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關C語言單鏈表如何實現通訊錄管理系統,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
保存人的信息有:
名字 name
電話 telephone
性別 sex
年齡 age
用一個結構體來裝這些信息:
struct infor{ char name[20]; int age; char sex[8]; char telephone[16]; };
實現功能有:
增加聯系人
刪除聯系人
修改聯系人
尋找聯系人
顯示聯系人
首先建立鏈表的基本功能創建頭鏈表,創建節點,插入節點
struct addre* Creathead(){ //創建頭鏈表 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){ //創建節點 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){ //插入節點 struct addre *t = headnode; while (t->next != NULL){ t = t->next; } struct addre *nodelist = Creatlist(list); t->next = nodelist; nodelist->next = NULL; }
然后在實現通訊錄的功能
void Addpeople(struct addre* node){ //添加人的信息 struct infor *a=malloc(sizeof(struct infor)); // 創建動態信息結構體指針 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; //要從頭節點的下一個節點顯示信息 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"); }
本人使用的時多文件的方式上面的代碼都在函數定義的源文件里實現
main函數的源文件代碼:
#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{//信息結構體 char name[20]; int age; char sex[8]; char telephone[16]; }; struct addre{ //鏈表 struct infor people; struct addre *next; }; //功能函數 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); //下面未鏈表函數 extern struct addre* Creatlist(struct infor *list); extern struct addre* Creathead(); extern void Addlist(struct addre *headnode, struct infor *list); #endif
關于“C語言單鏈表如何實現通訊錄管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。