单链表实现个人通讯录管理助手

通过调试,能够运行成功



#include<iostream>
#include<iomanip>
#include<windows.h>
using namespace std;

struct Phone
{
 char name[10];            //姓名
 char number[12];         //电话号码
 char add[30];           //住址
 char post[20];         //邮编
 int age;              //年龄
 char sex[2];         //性别
 char QQ[11];        //QQ
 char weixin[11];   //微信
 char birth[20];   //生日
 struct Phone *next;
};

typedef struct Phone Node;
typedef Node *pNode;

void Check(pNode pHead)
{
 pNode ptr;
 ptr=pHead;
 char ckName[10];
 cout<<"请输入您要查询人的姓名:"<<endl;
 cin>>ckName;
 while (ptr)
 {
  if (strcmp(ptr->name,ckName)==0)//strcmp是比较两个字符串是否相等
  {
   cout<<"您要查询的人的号码是:"<<ptr->number<<endl;
   break;
  }
  else
  {
   ptr=ptr->next;
   cout<<"您要查询的人的号码不存在!"<<endl;
  }

 }
}

void AddNewItem(pNode &pHead)
{
 pNode ptr;
 pNode newnode=new Node;
 ptr=NULL;
 cout<<"请输入您要添加人的信息:"<<endl;
 cout<<"姓名:";
 cin>>newnode->name;
 cout<<"电话号码:";
 cin>>newnode->number;
  cout<<"地址:";
 cin>>newnode->add;
  cout<<"邮编:";
 cin>>newnode->post;
  cout<<"年龄:";
 cin>>newnode->age;
  cout<<"性别:";
 cin>>newnode->sex;
  cout<<"QQ:";
 cin>>newnode->QQ;
  cout<<"微信:";
 cin>>newnode->weixin;
   cout<<"生日:";
 cin>>newnode->birth;

 if (pHead)
 {
  ptr=pHead;
  while (ptr->next)
  {
   ptr=ptr->next;
  }
  newnode->next=NULL;
  ptr->next=newnode;
 }
 else
 {
  newnode->next=pHead;
  pHead=newnode;
  ptr=pHead;
 }
 cout<<"添加成功!"<<endl;
}

void view(pNode pHead)
{
 pNode ptr;
 ptr=pHead;
 while (ptr)
 {
  cout<<"姓名:"<<ptr->name<<"\n";
  cout<<"电话:"<<ptr->number<<"\n";
  cout<<"地址:"<<ptr->add;
  cout<<"\n"<<"邮编:"<<ptr->post<<"\n";
  cout<<"年龄:"<<ptr->age<<"\n";
  cout<<"性别:"<<ptr->sex;
  cout<<"\n"<<"QQ  :"<<ptr->QQ<<"\n";
  cout<<"微信:"<<ptr->weixin<<"\n";
  cout<<"生日:"<<ptr->birth<<endl;
  ptr=ptr->next;
 }
}

void DeleteItem(pNode &pHead)
{
 pNode ptr,pBefore;
 ptr=pBefore=pHead;
 char ckName[10];
 cout<<"请输入您要删除人的姓名:"<<endl;
 cin>>ckName;
    while (ptr!=NULL)
    {
  if (strcmp(ptr->name,ckName)==0)
  {
   break;
  }
  else
  {
   pBefore=ptr;
   ptr=ptr->next;
  }
    }

 if (ptr)
 {
  if (ptr==pHead)
  {
   pHead=ptr->next;
   delete ptr;
  }
  else
  {
   pBefore->next=ptr->next;
   delete ptr;
  }
  cout<<"删除成功!"<<endl;
 }
}

 int main()
{

  int a=2;
  int b;
  char *user; //账号
  user=new char[10];
  char *password;  //密码
    password=new char[10];
  system("cls");
  cout<<setw(20)<<"*******************************************************************************"<<endl;
    cout<<setw(20)<<"*                             个人通讯录管理助手                              *"<<endl;
    cout<<setw(20)<<"*                                                                             *"<<endl;
    cout<<setw(20)<<"*                                                                             *"<<endl;
    cout<<setw(20)<<"*             个人账号:12345                  密码:12345                    *"<<endl;
    cout<<setw(20)<<"*                                                                             *"<<endl;
    cout<<setw(20)<<"*******************************************************************************"<<endl;
pr1:cout<<" 请输入管理员账号:";
  cin>>user;
    cout<<" 请输入管理员密码:";
  cin>>password;
  if(strcmp(user,"12345")==0&&strcmp(password,"12345")==0)
   {

    cout<<endl<<"     登陆成功!请稍等。。"<<endl;
  Sleep(500);
      system("cls");

       pNode pHead;
 pHead=NULL;
 int iChoice;
  cout<<"+-------------------------------------------+"<<endl;
  cout<<"|             1-> 添加信息                  |"<<endl;
  cout<<"|             2-> 查看信息                  |"<<endl;
  cout<<"|             3-> 查询信息                  |"<<endl;
  cout<<"|             4-> 删除信息                  |"<<endl;
  cout<<"|             5-> 退出                      |"<<endl;
  cout<<"|                                           |"<<endl;
  cout<<"+-------------------------------------------+"<<endl;
  cout<<"请输入您要的操作(0-5):";
 while (cin>>iChoice)
 {
  switch (iChoice)
  {
  case 1:
   AddNewItem(pHead);//添加记录
   break;
  case 2:
   cout<<"您要查看的记录有:"<<endl;
   view(pHead);//显示记录
   break;
  case 3:
   Check(pHead);//查找记录
   break;
  case 4:
   DeleteItem(pHead); //删除记录
   break;
  case 5:
   cout<<"退出成功!"<<endl;
   return 0;
  }
  cout<<"+-------------------------------------------+"<<endl;
  cout<<"|             1-> 添加信息                  |"<<endl;
  cout<<"|             2-> 查看信息                  |"<<endl;
  cout<<"|             3-> 查询信息                  |"<<endl;
  cout<<"|             4-> 删除信息                  |"<<endl;
  cout<<"|             5-> 退出                      |"<<endl;
  cout<<"|                                           |"<<endl;
  cout<<"+-------------------------------------------+"<<endl;
  cout<<"请输入您要的操作(0-5):";
 }
  }
  else
    {
       if (a==0)
       {
       cout<<endl<<"输入错误!按任意键退出"<<endl;
       exit(0);
       }
       else
    cout<<" 登陆失败!请重新输入管理员帐号/密码,您还有"<<a<<"次机会"<<endl;
       a--;
    goto pr1;
    return false;
    }
    return 0;
}


单链表可以用来实现员工通讯录管理系统。下面是一种可能的实现方式: 定义员工结构体: ``` typedef struct Employee { int id; // 员工编号 char name[20]; // 员工姓名 char phone[20]; // 员工电话 struct Employee *next; // 指向下一个员工的指针 } Employee; ``` 定义链表结构体: ``` typedef struct EmployeeList { Employee *head; // 链表头指针 int length; // 链表长度 } EmployeeList; ``` 初始化链表: ``` EmployeeList initEmployeeList() { EmployeeList list; list.head = NULL; list.length = 0; return list; } ``` 添加员工: ``` void addEmployee(EmployeeList *list, Employee employee) { Employee *newEmployee = (Employee*)malloc(sizeof(Employee)); *newEmployee = employee; newEmployee->next = NULL; if(list->head == NULL) { list->head = newEmployee; } else { Employee *p = list->head; while(p->next != NULL) { p = p->next; } p->next = newEmployee; } list->length++; } ``` 删除员工: ``` void deleteEmployee(EmployeeList *list, int id) { if(list->head == NULL) { return; } if(list->head->id == id) { Employee *temp = list->head; list->head = list->head->next; free(temp); list->length--; return; } Employee *p = list->head; while(p->next != NULL) { if(p->next->id == id) { Employee *temp = p->next; p->next = p->next->next; free(temp); list->length--; return; } p = p->next; } } ``` 查找员工: ``` Employee* findEmployee(EmployeeList *list, int id) { Employee *p = list->head; while(p != NULL) { if(p->id == id) { return p; } p = p->next; } return NULL; } ``` 遍历链表: ``` void traverse(EmployeeList *list) { Employee *p = list->head; while(p != NULL) { printf("id:%d name:%s phone:%s\n", p->id, p->name, p->phone); p = p->next; } } ``` 这样,我们就可以使用单链表实现员工通讯录管理系统了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值