/*
vc 6.0运行通过-原创
链表做的
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define ID struct id
struct id
{
char name[20];
char tele[20];
ID *next;
};
int pc=0;
ID *creat()
{
ID *p1,*p2,*head;
char str[20];
p1=p2=head=NULL;
printf("\t\t\t 开始输入记录(姓名 # 结束)!\n");
while(1)
{
printf("请输入姓名:\n");scanf("%s",str);getchar();
if(strcmp(str,"#")==0) break;
p1=(ID*)malloc(sizeof(ID));
strcpy(p1->name,str);
printf("请输入电话号码:\n");scanf("%s",p1->tele);getchar();
if(head==NULL)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
pc++;
}
p2->next=NULL;
return(head);
}
/*输入/添加记录*/
ID *insert(ID *head)
{
ID *temp,*p1,*p2;
printf("插入操作开始!!!\n");
temp=(ID *)malloc(sizeof(ID));
printf("请输入姓名:\n");scanf("%s",temp->name);getchar();
printf("请输入电话号码:\n");scanf("%s",temp->tele);getchar();
if (head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
}
p2->next=temp;
temp->next=p1;
}
printf("插入成功");
pc++;
return (head);
}
/*删除记录*/
ID *delet(ID *head)
{
ID *p1,*p2;
char str[20];
printf("请输入要删除的电话号码:");scanf("%s",str);getchar();
p1=head;
if (head==NULL)
{
printf("没有记录\n");
goto end;
}
while(p1!=NULL && strcmp(p1->tele,str))
{
p2=p1;p1=p1->next;
}
if(p1==NULL)
printf("未找到符合记录!\n");
else if(strcmp(p1->tele,str)==0)
{
if (p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("删除成功!!!\n");
pc--;
}
end:return head;
}
/*查找记录*/
ID *search(ID *head)
{
ID *p1,*p2;
int flag=0;
char c;
printf("请输入姓名的首字母:");scanf("%c",&c);getchar();
p1=head;
while( p1!=NULL)
{
if(p1->name[0]==c)
{
printf("姓名:%s\t电话号码:%s\n",p1->name,p1->tele);
flag=1;
}
p2=p1;p1=p1->next;
}
if(flag==0) printf("未找到符合记录!\n");
return head;
}
/*修改记录*/
ID *modify(ID *head)
{
ID *p1,*p2;
int mode;
char str[20];
printf("请输入要修改记录的姓名:");scanf("%s",str);getchar();
p1=head;
while( p1!=NULL)
{
if(strcmp(p1->name,str)==0)
{
printf("1.姓名:%s\t2.电话号码:%s\n",p1->name,p1->tele);
printf("请选择要修改选项:\n");
scanf("%d",&mode);getchar();
if(mode==1)
{
printf("请输入修改后的姓名\n");
scanf("%s",p1->name);getchar();
}
else if(mode==2)
{
printf("请输入修改后的电话号码\n");
scanf("%s",p1->tele);getchar();
}
else
printf("输入有误!\n");
break;
}
p2=p1;p1=p1->next;
}
if(p1==NULL) printf("未找到符合要求的记录!\n");
return head;
}
/*显示结果函数*/
void print(ID *head)
{
ID *p;
p=head;
printf("\t\t\t*****************\n");
printf("显示结果是:\n");
printf("姓名\t电话号码\n");
if(head!=NULL)
do
{
printf("%s\t%s\n",p->name,p->tele);
p=p->next;
} while(p!=NULL);
}
void main()
{
ID *head=NULL;
int choise;
printf("\t\t\t* * * * C语言课设* * * *\n");
while(1)
{
printf("\t\t 电话簿管理系统\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t 1.输入\n");
printf("\t\t 2.显示\n");
printf("\t\t 3.查找\n");
printf("\t\t 4.插入\n");
printf("\t\t 5.删除\n");
printf("\t\t 0.退出\n");
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("请选择(0-6):");
scanf("%d",&choise);getchar();
switch(choise)
{
case 1: head=creat();
break;
case 2: print(head);
break;
case 3: head=search(head);
break;
case 4: head=insert(head);
break;
case 5: head=delet(head);
break;
case 0:
exit(0);
break;
default :printf("输入错误,请重新输入!\n");
}
}
}
温馨提示:答案为网友推荐,仅供参考