C語(yǔ)言單鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)
本文實(shí)例為大家分享了C語(yǔ)言單鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
本人前幾天剛剛自學(xué)了單鏈表,趁熱打鐵,趕緊寫(xiě)一個(gè)小小的項(xiàng)目練練手。
單鏈表的實(shí)現(xiàn)在本人之前的博客中有:C語(yǔ)言編寫(xiě)一個(gè)鏈表
通訊錄管理系統(tǒng)
保存人的信息有:
名字 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
代碼可直接復(fù)制使用,如有不足請(qǐng)?zhí)岢觯罄m(xù)修改謝謝
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言通訊錄管理系統(tǒng)完整版
- C語(yǔ)言通訊錄管理系統(tǒng)課程設(shè)計(jì)
- C語(yǔ)言實(shí)現(xiàn)個(gè)人通訊錄管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)通訊錄管理系統(tǒng)
- 基于C語(yǔ)言實(shí)現(xiàn)個(gè)人通訊錄管理系統(tǒng)
- C語(yǔ)言通訊錄管理系統(tǒng)完整代碼
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)
- C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單通訊錄管理系統(tǒng)
- c語(yǔ)言實(shí)現(xiàn)通訊錄管理系統(tǒng)詳細(xì)實(shí)例
相關(guān)文章
C++實(shí)現(xiàn)簡(jiǎn)單通訊錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單通訊錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06c語(yǔ)言和c++語(yǔ)言中const修飾的變量區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于c語(yǔ)言和c++語(yǔ)言中const修飾的變量區(qū)別的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02C語(yǔ)言實(shí)現(xiàn)最大間隙問(wèn)題實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)最大間隙問(wèn)題的方法,是一個(gè)比較經(jīng)典的算法設(shè)計(jì)問(wèn)題,對(duì)于學(xué)習(xí)算法設(shè)計(jì)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-09-09C/C++?Qt實(shí)現(xiàn)文章小說(shuō)人物關(guān)系分析
這篇文章主要為大家詳細(xì)介紹了C/C++?Qt如何實(shí)現(xiàn)文章小說(shuō)人物關(guān)系分析功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01