13,825
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct
{
char num[5];
char name[20];
char phone[10];
}studata;
typedef struct node
{
studata data;
struct node *next;
}listnode,*linklist;
void menu()
{
printf("\n***************************班级通讯录***********************************\n\n");
printf("1-建立通讯录 2-查看全部记录 3-查询 4-同学加入 5-同学离开 0-退出系统 \n\n");
printf("请选择(0-5):");
}
linklist creatlist()
{
linklist head,s,p,q;
char ch;
head=(listnode*)malloc(sizeof(listnode));
if(!head) exit(-1);
head->next=NULL;
p=head;
do{
s=(linklist)malloc(sizeof(listnode));
if(!s) exit(-1);
printf("请输入同学的编号:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.num);
printf("请输入同学的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.name) ;
printf("请输入同学的电话号码:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.phone) ;
s->next=p->next;
p->next=s;
printf("\n继续添加吗?(y/n)");
fflush(stdout);rewind(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
listnode * searchlist(linklist head)
{
listnode *p;
int x;
char num[5];
char name[20];
if(head==NULL){
printf("无记录,请先添加记录!\n");
return NULL;
}
printf("1.按编号查询 2.按姓名查询\n");
printf("请选择:");
fflush(stdout);rewind(stdin);
scanf("%d",&x);
if(x==1)
{
printf("请输入待查同学的编号:");
fflush(stdout);rewind(stdin);
scanf("%s",num);
for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next);
}
else if(x==2)
{
printf("请输入待查同学的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",name);
for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next);
}
return p;
}
void insertlist(linklist head)
{
listnode *p;
if(head==NULL){
printf("无记录,请先添加记录!\n");
return;
}
printf("插入\n");
p=(listnode*)malloc(sizeof(listnode));
printf("请输入同学的编号:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.num);
printf("请输入同学的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.name) ;
printf("请输入同学的电话号码:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.phone) ;
p->next=head->next;
head->next=p;
}
void delnode(linklist head)
{
linklist p,q;
char ch;
if(head==NULL){
printf("无记录,请先添加记录!\n");
return;
}
p=searchlist(head);
if(!p)
{
printf("没有找到符合条件的记录!\n");
return ;
}
printf("确定要删除吗?(y/n)");
fflush(stdout);rewind(stdin);
scanf("%c",&ch);
if(ch=='n'||ch=='N') return;
for(q=head;q!=NULL&&q->next!=p;q=q->next);
q->next=p->next;
free(p);
printf("删除成功!\n");
}
void printlist(linklist head)
{
linklist p;
if(head==NULL){
printf("无记录,请先添加记录!\n");
return;
}
p=head->next;
printf("编号 姓名 电话号码\n");
while(p!=NULL)
{
printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone);
p=p->next;
}
}
int main()
{
int t;
linklist head=NULL,p=NULL;
menu();
while(1)
{
scanf("%d",&t);
switch(t)
{
case 1:
printf("建立通讯录\n");
head=creatlist();
break;
case 2:
printf("查看全部记录\n");
printlist(head);
break;
case 3:
printf("查询\n");
p=searchlist(head);
if(p)
{
printf("编号 姓名 电话号码\n");
printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone);
}else printf("查无此人!\n");
break;
case 4:
insertlist(head);
break;
case 5:
printf("删除\n");
delnode(head);
break;
case 0:
printf("退出\n");
return 0;
}
}
}