数据结构C语言编程—链表实现通讯录管理系统

#include<stdio.h>
#include<malloc.h>
#include<windows.h>
#include<string.h>
#define maxlen 100
#define LEN sizeof(struct client)
struct client /联系人信息/
{
char name[20];
char telephone[20];
char email[20];
struct client *next;
}*head,*p,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*cli;
int n;
int i;
struct client con[maxlen]; /信息缓冲结构体数组/

void save() /文件保存函数/
{
FILE*fp;
fp=fopen(“client.txt”,“w”); /创建文件/
p1=head;
for(i=0;p1!=NULL;i++,p1=p1->next) /向文件输出数据/
{
strcpy(con[i].name,p1->name);
strcpy(con[i].telephone,p1->telephone);
strcpy(con[i].email,p1->email);
fwrite(&con[i],sizeof(struct client),1,fp);
}
fclose(fp);
}

struct client creat() /创建函数/
{
n=0;
i=0;
p1=p2=(struct client
)malloc(LEN);
printf(“input the information:\n”);
scanf("%s",con[i].name); /输入第一个联系人信息/
scanf("%s",con[i].telephone);
scanf("%s",con[i].email);
strcpy(p1->name,con[i].name);
strcpy(p1->telephone,con[i].telephone);
strcpy(p1->email,con[i].email);
printf("\n==============\n");
head=NULL;
while(p1->name!=NULL) /循环判断是否继续输入/
{
n++;
i++;
if(n1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct client*)malloc(LEN);
scanf("%s",con[i].name);
if(strcmp(con[i].name,‘ok’)0)break; /输入OK,循环终止/
scanf("%s",con[i].telephone);
scanf("%s",con[i].email);
printf("\n
==========\n");
strcpy(p1->name,con[i].name);
strcpy(p1->telephone,con[i].telephone);
strcpy(p1->email,con[i].email);
}
p2->next=NULL;
printf(“press any to return main menu:”); /返回主菜单/
getchar();
getchar();
system(“cls”);
return head;
}

void print() /输出联系人信息函数/
{
printf("\nthe contact information :\n");
p=head;
while(p!=NULL)
{
printf("\n==============\n");
printf("\n\n\tname:%s\n",p->name);
printf("\n\n\ttelephone:%s\n",p->telephone);
printf("\n\n\temail:%s\n",p->email);
p=p->next;
}
printf(“press any key to return main menu:”); /返回主菜单/
getchar();
getchar();
system(“cls”);
}

void dele() /删除信息函数/
{
char del[20];
printf(“please input the name:”); /通过姓名查找/
scanf("%s",del);
p4=head;
if(strcmp(p4->name,del)==0)
{
p4=p4->next;
head=p4;
}
else{
while(strcmp(p4->next->name,del)!=0)
p4=p4->next;
p5=p5->next;
p4->next=p5->next;
free(p5);
}
print(); /输出删除后的剩余联系人信息/
}

struct client *add(struct client *cli) /联系人添加函数/
{
p6=head;
while(strcmp(cli->name,p6->name)>0) /判断信息插入位置/
{p7=p6;
p6=p6->next;
}
if(strcmp(cli->name,p6->name)<0)
{
if(head==p6)
{
head=cli;
cli->next=p6;
}
else
{
p7->next=cli;
cli->next=p6;
}
}
else
{
p6->next=cli;
cli->next=NULL;
}
n=n+1;
print(); /输出添加后的所有联系人信息/
system(“cls”);
return head;
}

void amend() /信息修改函数/
{
char nam[20];
int integer;
p1=head;
printf(“please input the name:\n”); /由姓名查找联系人信息/
scanf("%s",nam);
system(“cls”);
while(strcmp(p1->name,nam)!=0&&p1->next!=NULL)
p1=p1->next;
if((strcmp(p1->name,nam)0)||(p1->next=NULL))
{
printf("\t\tamend:\n"); /要修改的选项/
printf("\t\t\t1.name\n"); /修改姓名/
printf("\t\t\t2.telephone\n"); /修改电话号码/
printf("\t\t\t3.email\n"); /修改邮箱/
printf(“please input an integer between (1-3):\n”);
scanf("%d",&integer);
switch(integer)
{
case 1:{printf(“input name:\n”); /输入新的姓名/
scanf("%s",p1->name);
printf("\n
============\n"); /输出修改后的联系人信息/
printf("\n%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);}
break;
case 2:{printf(“input telephone:\n”); /输入新的电话号码/
scanf("\n%s",p1->telephone);
printf("\n==============\n"); /输出修改后的联系人信息/
printf("%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);}
break;
case 3:{printf(“input email:\n”); /输入新的邮箱/
scanf("\n%s",p1->email);
printf("\n==============\n"); /输出修改后的联系人信息/
printf("%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);}
break;
default:printf(“you input an error number”); /输入数字有误/
break;
}
}
}

struct client *demand(struct client *head) /信息查询函数/
{
int choice=1;
char nam[20];
char tele[20];
char em[20];
p1=head;
while(choice!=4)
{
system(“cls”);
printf(“telephone menu\n”); /查询选项/
printf(“1 demand by name\n”); /通过姓名查询/
printf(“2 demand by telephone number\n”); /通过电话号码/
printf(“3 demand by email\n”); /通过邮箱/
printf(“4 return to main menu\n”); /返回主菜单/
printf(“please choose(1-3):”);
scanf("%d",&choice);
if(choice4) /选项为4,返回主菜单/
{
system(“cls”);
return;
}
if(choice
1) /选项为1,通过姓名查询/
{
system(“cls”);
printf(“input the name:\n”);
scanf("%s",&nam);
if(head=NULL)
{
printf("\nlist null\n");
return head;
}
while(strcmp(p1->name,nam)!=0&&p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->name,nam)0)
{
printf("\n
============\n"); /输出联系人信息/
printf("%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);
}
else
printf(“no information of this person!”);
}
else if(choice2) /选项为2,通过电话号码查询/
{
system(“cls”);
printf(“input the telephone:\n”);
scanf("%s",&tele);
if(head=NULL)
{
printf("\nlist null\n");
return head;
}
while(strcmp(p1->telephone,tele)!=0&&p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->telephone,tele)0)
{
printf("\n
==========\n"); /输出联系人信息/
printf("%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);
}
else
printf(“no information of this person!”);
}
else if(choice3) /选项为3,通过邮箱查询/
{
system(“cls”);
printf(“input the em:\n”);
scanf("%s",&em);
if(head=NULL)
{
printf("\nlist null\n");
return head;
}
while(strcmp(p1->email,em)!=0&&p1->next!=NULL)
p1=p1->next;
if(strcmp(p1->email,em)0)
{
printf("\n
==========\n"); /输出联系人信息/
printf("%s\n",p1->name);
printf("%s\n",p1->telephone);
printf("%s\n",p1->email);
printf(“press any key to return main menu:”);
getchar();
getchar();
system(“cls”);
}
else
printf(“no information of this person!”);
}
}
return head;
}

void main() /主函数/
{
struct client *cli;
char choice[10]="";
head=NULL;
do
{
printf("\t\t=Phoon book number Query system=\n"); /通讯录菜单/
printf("\t\t\t1.Add a new contact\n");
printf("\t\t\t2.Modify contact’s information\n");
printf("\t\t\t3.Delete contact’s information\n");
printf("\t\t\t4.Contact information query\n");
printf("\t\t\t5.Contact information summery\n");
printf("\t\t\t6.Sign out\n");
printf("\t\t========\n");
printf("\t\t======please choose an integer between (1-6)😊; /功能选择/
scanf("%s",choice);
switch(choice[0])
{
case’1’:{system(“cls”);
if(head
NULL)head=creat(); /链表为空,调用create创建第一个联系人信息/
else{
printf(“input the information:\n”); /已存有联系人,调用add添加新的联系人/
cli=(struct client *)malloc(LEN);
scanf("%s",cli->name);
scanf("%s",cli->telephone);
scanf("%s",cli->email);
add(cli);}
save(); /调用save保存联系人信息/
break;}
case’2’:{system(“cls”);amend();save();} /调用amend 修改联系人信息,并将其保存/
break;
case’3’:{system(“cls”);dele();save();} /调用dele删除联系人信息,并将其保存/
break;
case’4’:{system(“cls”);demand(head);} /调用demand查询联系人信息/
break;
case’5’:{system(“cls”);print();} /调用print输出联系人信息/
break;
case’6’:return; /退出系统/
default:
break;
}
}
while(1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长风弦歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值