[Problem description]
Set up an employee Address Book management system for an organization, can easily query each employee's office phone, mobile phone number, and e-mail. Its functions include the establishment of the Contacts list, the query, modification, insertion and deletion of the employee's communication information, and the output of the entire Address Book table.
[Implementation Tips]
You can use a single-linked table storage structure, such as a storage structure that can be defined as follows:
typedef struct {/* structure type definition for employee communication information */
Char num[5]; /* Employee Number */
Char name[10]; /* Employee Name */
Char phone[15]; /* Office Phone number */
Char call[15]; /* Mobile number */
Char mail[25]; /* Mailbox */
}datatype;
/* node type for address list single-linked list */
typedef struct NODE
{DataType data; /* Data field for node */
struct node *next; /* The pointer field of the node */
}listnode,*linklist;
Source
Address_list1.cpp: Defines the entry point of the console application.
//
#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 {/* structure type definition for employee communication information */
Char num[5]; /* Employee Number */
Char name[10]; /* Employee Name */
Char phone[15]; /* Office Phone number */
Char call[15]; /* Mobile number */
Char mail[25]; /* Mailbox */
}datatype;
/* node type for address list single-linked list */
typedef struct NODE
{
DataType data; /* Data field for node */
struct node *next; /* The pointer field of the node */
}lnode, *linklist;
void CreateList (linklist &l)
{//reverse sequence input n element value, establish single-chain linear table with header node L
Linklist p;
int i,n;
L = (linklist) malloc (sizeof (Lnode));
L->next = NULL;
cout << "Please enter the number of communication information to create an employee:";
CIN >> N;
for (i = 0; i<n; i++) {
p = (linklist) malloc (sizeof (Lnode));
cout << "Please enter employee Information" <<endl;
cout << "Employee number:"; Cin>> p->data.num;
cout << "Employee Name:"; CIN >> p->data.name;
cout << "office phone number:"; CIN >> p->data.phone;
cout << "mobile phone number:"; CIN >> p->data.phone;
cout << "employee Email:"; CIN >> p->data.mail;
cout << "================================" << Endl;
P->next = l->next;
L->next = p;
}
}
void Initlist (linklist &l)
{//Initialize linear table
L = (linklist) malloc (sizeof (Lnode));
L->next = NULL;
}
void Destroylist (linklist &l)
{//Destroy linear table
Linklist p, q;
p = L;
Q = p->next;
while (q! = NULL)
{
Free (p);
}
}
int Listempty (linklist &l)
{//Determine if the linear table is empty
if (L->next = = NULL)
return TRUE;
Else
return FALSE;
}
int Listlength (linklist &l)
{//The length of the list
linklist p = L;
int c = 0;
while (p->next! = NULL) {
C + +;
p = p->next;
}
return (c);
}
void Getelem (linklist &l)
{//Fetch list I data elements
linklist p = l->next;
string S;
cout << "Enter the employee's number or name:";
Cin >> S;
while (P! = NULL)//Find employees based on relevant information.
{
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 << "No this man!" << Endl;
else{
cout << "Employee Information" << Endl;
cout << "Employee number:" << p->data.num << Endl;
cout << "Employee Name:" << p->data.name << Endl;
cout << "office phone Number:" << p->data.phone << Endl;
cout << "mobile phone Number:" << p->data.phone << Endl;
cout << "Employee Email:" << p->data.mail << Endl;
cout << "================================" << Endl;
}
}
void Reviselist (linklist &l)//Modify Information
{
linklist p = l->next;
Char j[20];
string S;
int i;
cout << "Enter the employee's number or name:";
Cin >> S;
while (P! = NULL) {//based on relevant information, find employees.
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 << "No this man!" << Endl;
Else
{
cout << "\ n want to change what information? _1-Number 2-Name 3-Office phone number 4-Mobile number 5-Email "<< Endl;
Cin >> I;
cout << "What do you want to change?" "<< 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 << "input error," << Endl;
System ("pause");
}
cout << "The change is complete! ";
System ("pause");
Return
}
}
void Listdelete (linklist &l)
Delete element I
{
Linklist p, q;
int j = 0,i;
p = L;
cout << "Please enter the information you want to delete the employee:";
Cin >> I;
while (P->next && J < i-1) {
p = p->next;
++j;
}
if (! ( P->next) | | J > I-1)//delete location unreasonable
cout << "Delete location unreasonable" << Endl;
Q = p->next;
P->next = q->next;//Delete and release nodes
Free (q);
}
void Listinsert (linklist &l)
{
linklist s, p = L;
s = (linklist) malloc (sizeof (Lnode));
cout << "Please enter employee Information" << Endl;
cout << "Employee number:"; CIN >> s->data.num;
cout << "Employee Name:"; CIN >> s->data.name;
cout << "office phone number:"; CIN >> s->data.phone;
cout << "mobile phone number:"; CIN >> s->data.phone;
cout << "employee Email:"; CIN >> s->data.mail;
cout << "================================" << Endl;
S->next = p->next;
P->next = s;
}
void Printlist (linklist &l)
Print a linear table
{
linklist p = l->next;
int i = 1;
if (p = = NULL)
cout << "The Address Book is empty!" "<< Endl;
while (P! = NULL)
{
cout << "First" <<i<< "Employee Information" << Endl;
cout << "Employee number:" << p->data.num << Endl;
cout << "Employee Name:" << p->data.name << Endl;
cout << "office phone Number:" << p->data.phone << Endl;
cout << "mobile phone Number:" << p->data.phone << Endl;
cout << "Employee Email:" << p->data.mail << Endl;
cout << "==============================" << Endl;
p = p->next;
i++;
}
}
void menu () {
cout << "=================================" << Endl;
cout << "1. Create a new Address book (reverse order creation)" << Endl;
cout << "2. Inquire about communications" << Endl;
cout << "3. Modify employee Information" << Endl;
cout << "4. Add employee Information" << Endl;
cout << "5. Delete Employee Information" << Endl;
cout << "6. Output all information" << Endl;
cout << "7. Number of contacts" << Endl;
cout << "0. Exit "<< Endl;
cout << "=================================" << Endl;
}
int _tmain (int argc, _tchar* argv[])
{
int in;
do{
System ("CLS");
Linklist L;
menu ();
cout << "Please enter the operation number:";
Cin >> in;
Switch (in)
{
Case 0:
Break
Case 1://Creating a new Address Book
CreateList (L);//inverse-order establishment of linear table
cout << "Create information successfully!" ";
System ("pause");
Break
Case 2: {//find
Getelem (L);
System ("pause");
Break
}
Case 3://Modify
Reviselist (L);
System ("pause");
cout << "Modify the information successfully!" ";
Break
Case 4://Add
Listinsert (L);
cout << "Add information successfully! ";
System ("pause");
Break
Case 5:
Printlist (L);
Listdelete (L);
cout << "Delete information successfully! ";
System ("pause");
break;//Output All information
Case 6:
Printlist (L);
System ("pause");
Break
Case 7:
cout << "This directory has" << listlength (L) << "Employee information! "<< Endl;;
System ("pause");
Break
Default
cout << "input error!" << Endl;
System ("pause");
}
} while (in! = 0);
}
Unit employee Address Book Management system (Application of linear table)