⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 通讯录.cpp

📁 是一个超市选地址的问题的算法
💻 CPP
字号:
#include<iostream.h>
#include<string.h>
#include<stdlib.h>





struct  DataType{       //通讯录结点类型
     int num;          //编号
     char name[9];     //姓名
     char sex[3];       //性别
     char phone[13];    //电话
     char addr[31];     //地址
};  
struct ListNode{               //结点类型定义
DataType data;                 //结点数据域
     ListNode *next;               //结点指针域
};
typedef ListNode *LinkList;
LinkList head;                   //定义指向单链表的头指针
ListNode *p;                    //定义一个指向结点的指针变量
int n=0;
int person[10];



int menu_select()                                          //
 {  //菜单选择函数程序
   int sn;
   cout<<"    通讯录管理系统"<<endl;
   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;
   cout<<"   请选择0-7:"<<endl;
   for(;;)
   {
      cin>>sn;
      if(sn<0 || sn>7)
	cout<<"输入错误,重选0-7:"<<endl;
      else
	break;
   }
     return sn;
 }


LinkList CreateList(void)                                  //
 {  //尾插法建立带头结点的通讯录链表算法
 LinkList head=new ListNode;    //申请头结点
 ListNode *rear;
 int flag=0;   //结束标志置0
 rear=head;  //尾指针初始指向头结点
 while(flag==0)
 {  p=(ListNode *)malloc(sizeof(ListNode));    //申请新结点
    cout<<"编号 姓名(8) 性别(2) 电话(11) 地址(31)"<<endl;
    cout<<"--------------------------------------"<<endl;
 cin>>p->data.num>>p->data.name>>p->data.sex>>p->data.phone>>p->data.addr;
n++;
    rear->next=p;   //新结点连接到尾结点之后
    rear=p;        //尾指针指向新结点
    cout<<"结束建表吗?(1/0):"<<endl;
    cin>>flag;    //读入一个标志数据
  }
    rear->next=NULL;    //终端结点指针域置空
    return head;          //返回链表头指针
 }


void InsertNode(LinkList head,ListNode *p)                    //
 {  //在通讯录链表head中插入结点
    ListNode *p1, *p2;
    p1=head;
    p2=p1->next;
    while(p2!=NULL && p2->data.num<p->data.num)
    { p1=p2;            //p1指向刚访问过的结点
     p2=p2->next;       //p2指向表的下一个结点
    } 
     p1->next=p;        //插入p所指向的结点
n++;
p->next=p2;        //连接表中剩余部分
 }


 ListNode *ListFind(LinkList head)                          //
 {  //有序通讯链表的查找
   ListNode *p;
   int num;
   char name[9];
   int xz;
   cout<<"====================="<<endl;
   cout<<"1.按编号查询 "<<endl;
   cout<<"2.按姓名查询 "<<endl;
   cout<<"====================="<<endl;
   cout<<"   请 选 择 : "<<endl;
   p=head->next;       //假定通讯录表带头结点
   cin>>xz;
   if(xz==1){
      cout<<"请输入要查找者的编号:"<<endl;
      cin>>num;
      while(p && p->data.num<num)
p=p->next;
      if(p==NULL || p->data.num>num)
  p=NULL;      //没有查到要查找的通讯者
   }
   else
   if(xz==2) {
   cout<<"请输入要查找者的姓名:"<<endl;
      cin>>name;
  while(p && strcmp(p->data.name,name)!=0)
 p=p->next; }
 return p;
 }


void DelNode(LinkList head)                                //
{   //通讯录链表上结点的删除	
   char jx;
   ListNode *p,*q;
   p=ListFind(head);   //调用查找函数
   if(p==NULL){
cout<<"没有查到要删除的通讯者!"<<endl;
  return;
   }
   cout<<"真的要删除该节点吗?(1/0):"<<endl;
   cin>>jx;
   if(jx==1){
   q=head;
while(q!=NULL && q->next!=p)
q=q->next;
q->next=p->next;     //删除结点
free(p);             //释放被删除的结点空间
n--;
cout<<"该通讯者已被删除!"<<endl;
   }
}


void PrintList(LinkList head)                               //
{   //通讯录链表的输出函数
ListNode *p;
p=head->next;     //因为链表带头结点,使p指向链表开始结点
cout<<"编号 姓 名  性别  联系电话  地 址"<<endl;
cout<<"-----------------------------------"<<endl;
while(p!=NULL)
{	
cout<<p->data.num<<" "<<p->data.name<<" "<<p->data.sex<<" "<<p->data.phone<<" "<<p->data.addr<<endl;
cout<<"----------------------------"<<endl;
p=p->next;      //后移一个结点
}
}


void Bubblesort(LinkList head)                             //
 {   //将带头结点的通讯录链表中各个结点按通讯者编号排序 
int i,j,k;
 ListNode *p;
     p=head->next;
for(i=1;i<=n;i++)
 { person[i]=p->data.num;
 p=p->next;
 }
  for(i=1;i<n;i++)
 {for(j=n;j>=i+1;j--)
     if(person[j]<person[j-1])
 {person[0]=person[j];
 person[j]=person[j-1];
              person[j-1]=person[0];
 }
   }
cout<<""<<endl;
cout<<"最终排序结果是:"<<endl;
for(k=1;k<=n;k++)
{cout<<person[k]<<endl;
p=head->next;
while(p->data.num!=person[k])
   p=p->next;
{cout<<p->data.name<<" "<<p->data.sex<<" "<<p->data.phone<<" "<<p->data.addr;}
cout<<"----------------------------"<<endl;
}
         }


   void main()                                              //
{
  for(;;){
     switch(menu_select())
     {
       case 1:
 cout<<"------------------------"<<endl;
cout<<"-  通讯录链表的建立  -"<<endl;
cout<<"------------------------"<<endl;
 head=CreateList();
 break;
       case 2:
 cout<<"------------------------"<<endl;
 cout<<"-   通讯者信息的添加   -"<<endl;
 cout<<"------------------------"<<endl;
 cout<<"编号 姓名(8) 性别 电话(11) 地址(31)"<<endl;
 cout<<"------------------------"<<endl;
p=new ListNode;
cin>>p->data.num>>p->data.name>>p->data.sex>>p->data.phone>>p->data.addr;
InsertNode(head,p);
break;
       case 3:
cout<<"------------------------"<<endl;
 cout<<"-   通讯录信息的查询   -"<<endl;
 cout<<"------------------------"<<endl;
 p=ListFind(head);
if(p!=NULL){
  cout<<"编号 姓名 性别 联系电话 地址"<<endl;
 cout<<"-----------------------"<<endl;
cout<<p->data.num<<p->data.name<<p->data.sex<<p->data.phone<<p->data.addr;
cout<<"-----------------------"<<endl;}
else
 cout<<"没查到要查询的通讯者!"<<endl;
 break;
       case 4:
cout<<"-------------------------"<<endl;
cout<<"-    通讯录信息的删除   -"<<endl;
cout<<"-------------------------"<<endl;
          DelNode(head);
          break;
       case 5:
cout<<"-------------------------"<<endl;
cout<<"-    通讯录链表的输出   -"<<endl;
cout<<"-------------------------"<<endl;
PrintList(head); 
break;
   case 6: 
cout<<"-------------------------"<<endl;
cout<<"-    求通讯者人数       -"<<endl;
cout<<"-------------------------"<<endl;
cout<<"通讯者人数为d"<<n<<endl;
         break;
 case 7:
cout<<"-------------------------"<<endl;
cout<<"-    按通讯者编号排序   -"<<endl;
cout<<"-------------------------"<<endl;
         Bubblesort(head);
break;
      case 0: 
cout<<"再  见  !"<<endl;
return;
      }
    }
 }





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -