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

b4ing 2009-07-03 02:57:11
#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的时候连添加都会出错 请问是什么原因?
...全文
135 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sou2012 2009-07-03
  • 打赏
  • 举报
回复
帮顶

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧