单位员工通讯录管理系统(线性表的应用)
[问题描述]
为某个单位建立一个员工通讯录管理系统,可以方便查询每一个员工的办公室电话、手机号、及电子邮箱。其功能包括通讯录链表的建立、员工通讯信息的查询、修改、插入与删除、以及整个通讯录表的输出。
[实现提示]
可以采用单链表的存储结构,如可定义如下的存储结构:
typedef struct { /*员工通讯信息的结构类型定义*/ char num[5]; /*员工编号*/ char name[10]; /*员工姓名*/ char phone[15]; /*办公室电话号码*/ char call[15]; /*手机号码*/ char mail[25]; /*邮箱*/ }DataType; /*通讯录单链表的结点类型*/ typedef struct node { DataType data; /*结点的数据域*/ struct node *next; /*结点的指针域*/ }ListNode,*LinkList;
源代码
// Address_List1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h" #include"stdio.h" #include "stdlib.h" # include <string> #include <malloc.h> #include<iostream> using namespace std; #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 typedef int Status; typedef struct { /*员工通讯信息的结构类型定义*/ char num[5]; /*员工编号*/ char name[10]; /*员工姓名*/ char phone[15]; /*办公室电话号码*/ char call[15]; /*手机号码*/ char mail[25]; /*邮箱*/ }DataType; /*通讯录单链表的结点类型*/ typedef struct node { DataType data; /*结点的数据域*/ struct node *next; /*结点的指针域*/ }LNode, *LinkList; void CreateList(LinkList &L) {//逆位序输入n个元素的值,建立带表头结点的单链线性表L LinkList p; int i,n; L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; cout << "请输入创建员工的通讯信息的个数:"; cin >> n; for (i = 0; i<n; i++){ p = (LinkList)malloc(sizeof(LNode)); cout <<" 请输入员工信息"<<endl; cout << " 员工编号:"; cin>> p->data.num; cout << " 员工姓名:"; cin >> p->data.name; cout << "办公室电话号码:"; cin >> p->data.phone; cout << " 手机号码:"; cin >> p->data.phone; cout << " 员工邮箱:"; cin >> p->data.mail; cout << "================================" << endl; p->next = L->next; L->next = p; } } void InitList(LinkList &L) {//初始化线性表 L = (LinkList)malloc(sizeof(LNode)); L->next = NULL; } void DestroyList(LinkList &L) {//销毁线性表 LinkList p, q; p = L; q = p->next; while (q != NULL) { free(p); } } int ListEmpty(LinkList &L) {//判断线性表是否为空 if (L->next == NULL) return TRUE; else return FALSE; } int ListLength(LinkList &L) {//求链表的长度 LinkList p = L; int c = 0; while (p->next != NULL){ c++; p = p->next; } return (c); } void GetElem(LinkList &L) {//取链表第i个数据元素 LinkList p = L->next; string s; cout << "输入员工的编号或名字:"; cin >> s; while (p != NULL)//根据相关信息,查找员工。 { if (p->data.num == s || p->data.name == s || p->data.phone == s || p->data.call == s || p->data.mail == s) break; p = p->next; } if (!p) cout << "查无此人!" << endl; else{ cout << " 员工信息" << endl; cout << " 员工编号:" << p->data.num << endl; cout << " 员工姓名:" << p->data.name << endl; cout << "办公室电话号码:" << p->data.phone << endl; cout << " 手机号码:" << p->data.phone << endl; cout << " 员工邮箱:" << p->data.mail << endl; cout << "================================" << endl; } } void ReviseList(LinkList &L)//修改信息 { LinkList p = L->next; char j[20]; string s; int i; cout << "输入员工的编号或名字:"; cin >> s; while (p != NULL) {//根据相关信息,查找员工。 if (p->data.num == s || p->data.name == s || p->data.phone == s || p->data.call == s || p->data.mail == s) break; p = p->next; } if (!p) cout << "查无此人!" << endl; else { cout << "\n想修改什么信息?_1-编号 2-姓名 3-办公室电话号码 4-手机号码 5-邮箱" << endl; cin >> i; cout << "想修改成什么?" << endl; cin >> j; switch (i) { case 1:strcpy_s(p->data.num, j); break; case 2:strcpy_s(p->data.name, j); break; case 3:strcpy_s(p->data.phone, j); break; case 4:strcpy_s(p->data.call, j); break; case 5:strcpy_s(p->data.mail, j); break; default: cout << "输入错误," << endl; system("pause"); } cout << "修改完毕!"; system("pause"); return; } } void ListDelete(LinkList &L) //删除第i个元素 { LinkList p, q; int j = 0,i; p = L; cout << "请输入你要删除第几个员工的信息:"; cin >> i; while (p->next && j < i - 1){ p = p->next; ++j; } if (!(p->next) || j > i - 1)//删除位置不合理 cout << "删除位置不合理" << endl; q = p->next; p->next = q->next;//删除并释放结点 free(q); } void ListInsert(LinkList &L) { LinkList s, p = L; s = (LinkList)malloc(sizeof(LNode)); cout << " 请输入员工信息" << endl; cout << " 员工编号:"; cin >> s->data.num; cout << " 员工姓名:"; cin >> s->data.name; cout << "办公室电话号码:"; cin >> s->data.phone; cout << " 手机号码:"; cin >> s->data.phone; cout << " 员工邮箱:"; cin >> s->data.mail; cout << "================================" << endl; s->next = p->next; p->next = s; } void PrintList(LinkList &L) //打印线性表 { LinkList p = L->next; int i = 1; if (p == NULL) cout << "通讯录为空!" << endl; while (p != NULL) { cout << "第 "<<i<<" 个员工信息" << endl; cout << " 员工编号:" << p->data.num << endl; cout << " 员工姓名:" << p->data.name << endl; cout << "办公室电话号码:" << p->data.phone << endl; cout << " 手机号码:" << p->data.phone << endl; cout << " 员工邮箱:" << p->data.mail << endl; cout << "==============================" << endl; p = p->next; i++; } } void menu(){ cout << "================================="<< endl; cout << " 1.建立新通讯录(逆序创建) " << endl; cout << " 2.查询通讯信息 " << endl; cout << " 3.修改员工信息 " << endl; cout << " 4.添加员工信息 " << endl; cout << " 5.删除员工信息 " << endl; cout << " 6.输出全部信息 " << endl; cout << " 7.通讯录的人数 " << endl; cout << " 0. 退出 " << endl; cout << "=================================" << endl; } int _tmain(int argc, _TCHAR* argv[]) { int in; do{ system("cls"); LinkList L; menu(); cout << "请输入操作序号:"; cin >> in; switch (in) { case 0: break; case 1://建立新通讯录 CreateList(L);//逆位序建立线性表 cout << "创建信息成功!"; system("pause"); break; case 2: {//查找 GetElem(L); system("pause"); break; } case 3: //修改 ReviseList(L); system("pause"); cout << "修改信息成功!"; break; case 4: //添加 ListInsert(L); cout << "添加信息成功!"; system("pause"); break; case 5: PrintList(L); ListDelete(L); cout << "删除信息成功!"; system("pause"); break;//输出全部信息 case 6: PrintList(L); system("pause"); break; case 7: cout << "该通讯录共有 " << ListLength(L) << " 员工信息!" << endl;; system("pause"); break; default: cout << "输入错误!" << endl; system("pause"); } } while (in != 0); }