请教一个关于C语言的链表通讯录的问题

b4ing b4ing 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include "stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"string.h"
typedef struct book
{
char name[30];
char sex;
char phone[30];
char address[30];
char other[30];
struct book *next;
}BOOK;

BOOK* creat()
{
BOOK *head;
head=(BOOK *)malloc(sizeof(BOOK));
printf("============================\n");
printf("Input the name: ");
scanf("%s",head->name);
getchar();
printf("Input the sex ");
scanf("%c",&head->sex);
printf("Input the phone: ");
scanf("%s",head->phone);
printf("Input address: ");
scanf("%s",head->address);
printf("Input other: ");
scanf("%s",head->other);
return head;
}

BOOK* insert(BOOK *head)
{
BOOK *p,*p1,*p2;
p=(BOOK *)malloc(sizeof(BOOK));
printf("============================\n");
printf("Input the name: ");
scanf("%s",p->name);
getchar();
printf("Input the sex ");
scanf("%c",&p->sex);
printf("Input the phone: ");
scanf("%s",p->phone);
printf("Input address: ");
scanf("%s",p->address);
printf("Input other: ");
scanf("%s",p->other);
p1=head;
while(p1->next!=NULL&&(strcmp(p->name,p1->name)>0))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p->name,p1->name)<0)
{
if(p1==head)
head=p;
else
p2->next=p;
p->next=p1;
}
else
{
p1->next=p;
p->next=NULL;
}

return head;
}

void print(BOOK *head)
{
BOOK *p;
p=head;
do
{
printf("%s\t",p->name);
printf("%c\t",p->sex);
printf("%s\t",p->phone);
printf("%s\t",p->address);
printf("%s\t",p->other);
printf("\n");
p=p->next;
}while(p!=NULL);
}


main()
{
BOOK *head;
int i;
char s;
head=(BOOK *)malloc(sizeof(BOOK));
do
{
i=1;
printf("======================================\n");
printf("0.Exti\n");
printf("1.Creat\n");
printf("2.Output\n");
printf("Input your select:\t");
scanf("%d",&s);
switch(s)
{
case 0:i=0;break;
case 1:if(head==NULL)
{head=creat();break;}
else
{head=insert(head);break;}
case 2:print(head);break;
}
}while(i==1);
getch();
}

我用WIN-TC可以添加通讯录 但是用VC 6的时候连添加都会出错 请问是什么原因?
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
b4ing
C语言 70,040 成员 243,237 内容