通讯录管理系统的设计与实现
设计要求:系统包括通讯者结点信息的插入、查询、删除、 更新以及通讯录信息的输出等功能。 菜单内容:
通讯者信息的插入通讯者信息的删除通讯者信息的查询通讯者信息的修改通讯录链表的输出0. 退出管理系统 请选择:1 – 5或者0:
使用单链表实现该功能:
知识点:指针的灵活运用
结点:
数据域:存储数据元素信息的域
指针域:存储直接后继位置的域
先做头文件
#include<stdio.h> #include<string.h> #include<stdlib.h> #pragma warning(disable:4996) void get(struct People* peoples);//输入通讯者信息 void set(struct People* peoples);//输出通讯者信息 void add(struct People** people);//通讯者信息的插入 void delete(struct Poleop** people);//通讯者信息的删除 void update(struct Poleop* people);//通讯者信息的修改 struct People* find(struct Poleop* people);//通讯者信息的查询 void see(struct People* peoples);//输出满足条件的通讯者信息 void frees(struct People** people);//释放 struct People { char name[10]; char telephone[11]; struct People* next; }; int main() { printf("##########################################\n"); printf("##### 1.add user 2.delete user######\n"); printf("##### 3.find user 4.update user######\n"); printf("##### 5.all user 0.exit ###########\n"); printf("#######################################\n"); int ch; int isgo = 1; struct People* people = NULL, * peoples; while (isgo) { printf("enter your action:"); scanf("%d", &ch); switch (ch) { case 1: add(&people); break; case 2: delete(&people); break; case 3: peoples = find(people); if (peoples == NULL) { printf("no finded!"); } else { printf("find it...\n"); see(peoples); } break; case 4: update(people); break; case 5: set(people); break; case 0: isgo = 0; break; default: break; } } frees(&people); return 0; } void add(struct People** people)//通讯者信息的插入 { struct People* peoples; static struct People* temp; peoples = (struct People*)malloc(sizeof(struct People)); if (peoples == NULL) { printf("Failed to allocate space.\n"); exit(1); } get(peoples); if (*people != NULL) { temp->next = peoples; peoples->next = NULL; } else { peoples->next = NULL; *people = peoples; } temp = peoples; } void update(struct Poleop* people)//通讯者信息的修改 { struct People* peoples; peoples = find(people); if (peoples == NULL) { printf("no find it!\n"); } else { printf("enter your telephone"); scanf("%s", peoples->telephone); } } void get(struct People* peoples) { printf("enter name:"); scanf("%s", peoples->name); printf("enter telephone:"); scanf("%s", peoples->telephone); } struct People* find(struct People* people)//通讯者信息的查询 { struct People* current; printf("enter your name:"); char news[20]; scanf("%s", news); current = people; while (current != NULL && strcmp(current->name, news)) { current = current->next; } return current; } void delete(struct Poleop** people)//通讯者信息的删除 { struct People* temp, * peoples, * current, * previous; peoples = find(*people); if (peoples == NULL) { printf("no finded it!\n"); } else { current = *people; previous = NULL; while (current != NULL && current != peoples) { previous = current; current = current->next; } if (previous == NULL) { *people = current->next; } else { previous->next = current->next; } free(peoples); } } void set(struct People* people) { struct People* peoples; int count = 1; peoples = people; while (peoples != NULL) { printf("%d: s", count); printf("name:%s\n", peoples->name); printf(" telephone:%s\n", peoples->telephone); peoples = peoples->next; count++; } } void see(struct People* peoples) { printf("name:%s\n", peoples->name); printf("telephone:%s\n", peoples->telephone); } void display(struct People* people) { struct People* current; current = people; while (current != NULL) { set(current); current = current->next; } } void frees(struct People** people) { struct People* temp; while (*people != NULL) { temp = *people; *people = (*people)->next; free(temp); } }