📄 txl.cpp
字号:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
struct Mas
{
char Num[5]; //编号
char Name[9]; //姓名
char Sex[3]; //性别
char Phone[13]; //电话
char Addr[31]; //地址
struct Mas *next; //下一个节点的地址
};
//初始化
void intsquse(Mas *&h)
{
h=new Mas;
h->next=NULL;
}
//通讯录链表的建立
void creat(Mas *&h)
{
int i=0;
Mas *p,*q;
p=q=NULL;
h->Num[1]=i;
h->next=q;
p=new Mas;
if(p==0)
{
cout<<"节点申请失败";
exit(1);
}
cout<<endl<<"请按下面的格式输入一个人的信息,f回车表示结束创建。";
cout<<endl<<"序号 姓名 性别 电话 地址"<<endl;
cin>>p->Num>>p->Name>>p->Sex>>p->Phone>>p->Addr;
p->next=NULL;
q=p;
while(1)
{
i++;
if(i==1)
h->next=q;
else
{
h->Num[1]=i;
q->next=p;
}
q=p;
p=new Mas;
cout<<endl<<"请按下面的格式再输入一个人的信息,f回车表示结束创建。";
cout<<endl<<"序号 姓名 性别 电话 地址"<<endl;
cin>>p->Num;
if(strcmp(p->Num, "f")==0)
break;
cin>>p->Name>>p->Sex>>p->Phone>>p->Addr;
p->next=NULL;
}
delete(p);
}
// 接点的输出
void prin(Mas *h)
{
int i=0;
if(h->next==NULL)
{
cout<<endl<<"通讯录为空!"<<endl;
exit(1);
}
Mas *p;
p=h->next;
cout<<endl<<"此通讯录中有:"<<endl;
while(p!=NULL)
{
i++;
cout<<p->Num<<"---"<<p->Name<<"----"<<p->Sex<<"----"<<p->Phone<<"----"<<p->Addr<<endl;
p=p->next;
}
cout<<"共有"<<"["<<i<<"]"<<"人。"<<endl;
}
//查找
Mas *reser(Mas *h)
{
Mas *p;
char c[10];
if(h->next==NULL)
{
cout<<endl<<"通讯录为空!";
exit(1);
}
p=h->next;
cout<<"请输入您要查找者的编号"<<endl;
cin>>c;
while(p!=NULL&&strcmp(p->Num,c)!=0)
p=p->next;
cout<<endl<<"您要查找的通讯者为:"<<endl<<p->Num<<"---"<<p->Name<<"----"<<p->Sex<<"----"<<p->Phone<<"----"<<p->Addr<<endl;
return p;
}
//插入
int insert(Mas *&h)
{
if(h->next==NULL)
{
cout<<endl<<"通讯录为空!";
return 1;
}
Mas *p=NULL;
Mas *q=NULL;
cout<<"(插入者在指定者之后)";
cout<<endl<<"请输入您指定者的序号"<<endl;
char c[10];
q=h->next;
cin>>c;
while(q!=NULL&&strcmp(q->Num,c)!=0)
q=q->next;
if(q==NULL)
{
cout<<"查无此人"<<endl;
return 1;
}
p=new Mas;
if(p==0)
{
cout<<"接点申请失败";
exit(1);
}
cout<<endl<<"请输入您要插入者的信息,并按下面的格式输入!";
cout<<endl<<"序号 姓名 性别 电话 地址"<<endl;
cin>>p->Num>>p->Name>>p->Sex>>p->Phone>>p->Addr;
p->next=q->next;
q->next=p;
cout<<endl<<"插入成功";
prin(h);
return 1;
}
//节点的删除
int dele(Mas *&h)
{
if(h->next==NULL)
{
cout<<endl<<"通讯录为空!";
return 1;
}
Mas *p=NULL,*q=NULL;
char c[10];
cout<<endl<<"请输入您要删除者的编号"<<endl;
cin>>c;
q=p=h->next;
if(strcmp(p->Num,c)==0)
{
h->next=p->next;
return 1;
}
while(p!=NULL&&strcmp(p->Num,c)!=0)
{
q=p;
p=p->next;
}
if(p==NULL)
{
cout<<"此人不存在"<<endl;
return 1;
}
q->next=p->next;
cout<<"删除成功"<<endl;
prin(h);
return 1;
}
//主函数
void main()
{
printf("\n-----欢迎使用通讯录------\n");
printf("_______________________________________________________");
printf("\n 1 通讯录链表的建立");
printf("\n 2 通讯录节点的插入");
printf("\n 3 通讯录节点的删除");
printf("\n 4 通讯录节点的查询");
printf("\n 5 通讯录节点的输出");
printf("\n 0 退出管理系统 ");
printf("\n_______________________________________________________");
Mas *head=NULL;
Mas *p=NULL;
intsquse(head);
while(1)
{
printf("\n注意!请选择0——5进行相应操作\n");
char c;
cin>>c;
switch(c)
{
case'1':
cout<<"您选择了创建操作"<<endl;
creat(head);
break;
case'2':
cout<<"您选择了输出操作"<<endl;
prin(head);
break;
case'3':
cout<<"您选择了查找操作"<<endl;
p=reser(head);
break;
case'4':
cout<<"您选择了插入操作";
insert(head);
break;
case'5':
cout<<"您选择了删除操作";
dele(head);
break;
case'0': exit(1);
default:
cout<<"\n选择错误,请重新输入"<<endl;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -