您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)C語(yǔ)言單鏈表如何實(shí)現(xiàn)通訊錄管理系統(tǒng),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
保存人的信息有:
名字 name
電話 telephone
性別 sex
年齡 age
用一個(gè)結(jié)構(gòu)體來(lái)裝這些信息:
struct infor{ char name[20]; int age; char sex[8]; char telephone[16]; };
實(shí)現(xiàn)功能有:
增加聯(lián)系人
刪除聯(lián)系人
修改聯(lián)系人
尋找聯(lián)系人
顯示聯(lián)系人
首先建立鏈表的基本功能創(chuàng)建頭鏈表,創(chuàng)建節(jié)點(diǎn),插入節(jié)點(diǎn)
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é)點(diǎn) 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é)點(diǎn) struct addre *t = headnode; while (t->next != NULL){ t = t->next; } struct addre *nodelist = Creatlist(list); t->next = nodelist; nodelist->next = NULL; }
然后在實(shí)現(xiàn)通訊錄的功能
void Addpeople(struct addre* node){ //添加人的信息 struct infor *a=malloc(sizeof(struct infor)); // 創(chuàng)建動(dòng)態(tài)信息結(jié)構(gòu)體指針 if (a == NULL){ printf("malloc error\n"); } printf("請(qǐng)輸入名字\n"); scanf("%s", a->name); printf("請(qǐng)輸入年齡\n"); scanf("%d", &a->age); printf("請(qǐng)輸入性別\n"); scanf("%s", a->sex); printf("請(qǐng)輸入電話號(hào)碼\n"); scanf("%s", a->telephone); Addlist(node, a); //用尾插法插入該人信息 printf("添加成功!\n"); } void Deletepeople(struct addre *node){ //刪除人的信息 char *str = malloc(sizeof(char)* 10); if (str == NULL){ //通過(guò)名字尋找 printf("malloc error\n"); } printf("請(qǐng)輸入要?jiǎng)h除人的姓名\n"); scanf("%s", str); struct addre *strat = node; struct addre *end = node->next; int flag = 0; //判斷是否找到 0為未找到,1 找到 while (end){ //判斷end的 不然會(huì)越界 if (strcmp(end->people.name, str) == 0){ flag = 1; break; } node = node->next; //到下一個(gè)鏈表 strat = node; //一個(gè)指向前面 一個(gè)指向后面,刪除將end刪除,前面那個(gè)直接指向end的指向 end = node->next; } if (flag){ strat->next = end->next; printf("刪除成功\n"); free(end); } else{ printf("沒(méi)找到!\n"); } } void Modifyinfor(struct addre *node){ //修改人的信息 char *str = malloc(sizeof(char)* 10); //通過(guò)名字尋找 if (str == NULL){ printf("malloc error\n"); } printf("請(qǐng)輸入要修改人的姓名\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("請(qǐng)重新輸入該人信息\n"); printf("請(qǐng)輸入名字\n"); scanf("%s", node->people.name); printf("請(qǐng)輸入年齡\n"); scanf("%d", &node->people.age); printf("請(qǐng)輸入性別\n"); scanf("%s", node->people.sex); printf("請(qǐng)輸入電話號(hào)碼\n"); scanf("%s", node->people.telephone); printf("修改成功\n"); } else{ printf("沒(méi)找到\n"); } } void Foundpeople(struct addre *node){ //找到某人的信息并打印出來(lái) char *str = malloc(sizeof(char)* 10); //通過(guò)名字尋找 if (str == NULL){ printf("malloc error\n"); } printf("請(qǐng)輸入要查找人的姓名\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("沒(méi)找到!\n"); } } void Display(struct addre *node){ struct addre *pmove = node->next; //要從頭節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)顯示信息 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í)多文件的方式上面的代碼都在函數(shù)定義的源文件里實(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
關(guān)于“C語(yǔ)言單鏈表如何實(shí)現(xiàn)通訊錄管理系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。