📄 通讯录.cpp
字号:
#include<iostream.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(liudaibo)
struct liudaibo
{
char name[20];
char telnum[20];
struct liudaibo* next;
};
liudaibo* xinjian();
liudaibo* charu();
liudaibo* shanchu();
liudaibo* chaxun();
liudaibo*shuchu();
liudaibo* head;
void main()
{
int shu;
do{
cout<<"请输入要执行的操作\n1新建\n2插入\n3查询\n4删除\n5输出\n0退出:";
cin>>shu;
if(shu==1) xinjian();
}while(shu!=1);
do{
cout<<endl<<"请输入要执行的操作\n2插入\n3查询\n4删除\n5输出\n0退出:";
cin>>shu;
if(shu==2) charu();
else if(shu==3) chaxun();
else if(shu==4) shanchu();
else if(shu==5) shuchu();
else if(shu==0) break;
}while(shu!=0);
}
liudaibo* xinjian()
{
liudaibo *p1,*p2;
head=NULL;
int n;
int NUM;
cout<<endl<<"请输入要创建的朋友个数:";
cin>>NUM;
for(n=1;n<=NUM;n++)
{
p1=(liudaibo*)malloc(LEN);
cout<<endl<<endl<<"请输入其姓名:";
cin>>p1->name;
cout<<"请输入其电话:";
cin>>p1->telnum;
if(n==1) head=p1;
else if(n==2)
{
head->next=p1;
p2=head->next;
p2->next=NULL;
}
else
{
p2->next=p1;
p2=p2->next;
p2->next=NULL;
}
}
return(head);
}
liudaibo* charu()
{
liudaibo *p0,*p1;
p0=(liudaibo*)malloc(LEN);
p1=head;
cout<<endl<<"请输入其姓名:";
cin>>p0->name;
cout<<"请输入其电话:";
cin>>p0->telnum;
if(head==NULL)
{
head=p0;
head->next=NULL;
}
else
{
while(p1->next!=NULL)
{
p1=p1->next;
}
p1->next=p0;
p0->next=NULL;
}
cout<<"插入成功"<<endl;
return(head);
}
liudaibo* shanchu()
{
liudaibo *p1,*p2;
if(head==NULL)
{
cout<<"通讯录为空,不可删除!"<<endl;
return(head);
}
p1=head;
char dname[20];
cout<<endl<<"请输入要删除朋友的姓名:";
cin>>dname;
while(strcmp(p1->name,dname)!=0 && p1->next!=NULL)
{
p2=p1;
p1=p2->next;
}
if(strcmp(p1->name,dname)==0 )
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
cout<<"成功删除!"<<endl;
}
else
cout<<dname<<"没有找到!"<<endl;
return(head);
}
liudaibo* chaxun()
{
liudaibo* p1;
p1=head;
char qname[20];
cout<<endl<<"请输入要查询朋友的姓名:";
cin>>qname;
while(strcmp(p1->name,qname)!=0 && p1->next!=NULL)
{
p1=p1->next;
}
if(strcmp(p1->name,qname)==0 )
{
cout<<p1->name<<endl;
cout<<p1->telnum<<endl;
}
else
cout<<"没找到"<<qname<<endl;
return(head);
}
liudaibo* shuchu()
{
liudaibo* p;
p=head;
if(head==NULL) cout<<"通讯录为空"<<endl;
while(p!=NULL)
{
cout<<endl<<p->name<<endl;
cout<<p->telnum<<endl<<endl;
p=p->next;
}
return(head);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -