关于C语言的链表问题
源程序:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLINE 60
struct birthday
{
int year,month,day;
};
struct address
{
char province[MAXLINE],city[MAXLINE],road[MAXLINE];
int code;
char zip[MAXLINE];
};
struct friends
{
char name[MAXLINE];
int age;
char telephone[MAXLINE];
struct birthday bir;
struct address add;
struct friends *next;
};
struct friends *Create(struct friends *head);
struct friends *Insert(struct friends *head,struct friends *fri);
struct friends *Delete(struct friends *head,char name[]);
void Print(struct friends *head);
int main(void)
{
struct friends *head=NULL,*p;
char name[MAXLINE];
int choice;
while(1)
{
printf("功能选项:1.新建\t2.插入\t3.删除\t4.显示所有信息\t0.退出\n");
printf("选择功能:");
scanf("%d",&choice);
if(choice==1)
head=Create(head);
else if(choice==2)
{
p=(struct friends *)malloc(sizeof(struct friends));
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入年龄:");
scanf("%d",&p->age);
printf("请输入电话号码:");
scanf("%s",p->telephone);
printf("请输入出生年份:");
scanf("%d",&p->bir.year);
printf("请输入出生月份:");
scanf("%d",&p->bir.month);
printf("请输入出生日期:");
scanf("%d",&p->bir.day);
printf("请输入联系地址(省、市、街道、门牌号):");
scanf("%s%s%s%d",p->add.province,p->add.city,p->add.road,&p->add.code);
getchar();
printf("请输入邮政编码:");
gets(p->add.zip);
head=Insert(head,p);
}
else if(choice==3)
{
printf("请输入要删除的联系人姓名:");
scanf("%s",name);
head=Delete(head,name);
}
else if(choice==4)
Print(head);
else if(choice==0)
break;
}
printf("\n");
return 0;
}
struct friends *Create(struct friends *head)
{
struct friends *p,*temp;
p=(struct friends *)malloc(sizeof(struct friends));
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入年龄:");
scanf("%d",&p->age);
printf("请输入电话号码:");
scanf("%s",p->telephone);
printf("请输入出生年份:");
scanf("%d",&p->bir.year);
printf("请输入出生月份:");
scanf("%d",&p->bir.month);
printf("请输入出生日期:");
scanf("%d",&p->bir.day);
printf("请输入联系地址(省、市、街道、门牌号):");
scanf("%s%s%s%d",p->add.province,p->add.city,p->add.road,&p->add.code);
getchar();
printf("请输入邮政编码:");
gets(p->add.zip);
p->next=NULL;
if(head==NULL)
head=p;
else
temp->next=p;
temp=p;
return head;
}
struct friends *Insert(struct friends *head,struct friends *fri)
{
if(head==NULL)
{
head=fri;
fri->next=NULL;
}
else
{
struct friends *temp1,*temp2;
char a[3],b[3];
int count=0;
temp1=head;
temp2=temp1->next;
a[0]=fri->name[0];
a[1]=fri->name[1];
a[2]='\0';
b[0]=temp1->name[0];
b[1]=temp1->name[1];
b[2]='\0';
while(1)
{
if(strcmp(a,b)==0)
{
temp1->next=fri;
fri->next=temp2;
count++;
break;
}
temp1=temp2;
temp2=temp1->next;
if((temp1!=head)&&(temp2==NULL))
break;
}
if(count==0)
{
temp2=fri;
fri->next=NULL;
}
}
return head;
}
struct friends *Delete(struct friends *head,char name[])
{
int count=0;
struct friends *temp,*temp1,*temp2;
if(head==NULL)
{
printf("查无此人!\n");
return NULL;
}
else if((head->next==NULL)&&(strcmp(name,head->name))!=0)
printf("查无此人!\n");
else if((head->next==NULL)&&(strcmp(name,head->name)==0))
free(head);
else if((head->next!=NULL)&&(strcmp(name,head->name)==0))
{
temp=head;
head=temp->next;
free(temp);
}
else
{
temp1=head;
temp2=temp1->next;
while(1)
{
if(strcmp(name,temp2->name)==0)
{
temp1->next=temp2->next;
free(temp2);
count++;
break;
}
temp1=temp2;
temp2=temp1->next;
if(temp2==NULL)
break;
}
if(count==0)
printf("查无此人!\n");
}
return head;
}
void Print(struct friends *head)
{
struct friends *i;
printf("显示所有信息:");
if(head==NULL)
{
printf("信息为空!\n");
}
printf(" 姓名 年龄 出生年月日 电话号码 邮编 通信地址\n");
for(i=head;i!=NULL;i=i->next)
{
printf("%s%4d%3d年%d月%d日%6s%6s%6s%s%s%d号\n",i->name,i->age,i->bir.year,i->bir.month,i->bir.day,i->telephone,i->add.zip,i->add.province,i->add.city,i->add.road,i->add.code);
}
}
麻烦各位大神看看我这个错在哪?运行程序时,只能用一次新建功能或者是插入功能,第二次新建时最后会出错。怎么回事?小弟是自学C语言的。不太懂。