C语言通讯录,求大神帮忙查错!
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#define LEN sizeof(struct basic_meg)
struct basic_meg *New(void)
void List(struct basic_meg *head);
void Search(struct basic_meg *head);
struct basic_meg *Delete(struct basic_meg *head);
struct basic_meg *Insert(struct basic_meg *head);
struct basic_meg
{
char name[20];
char number[20];
char email[20];
struct basic_meg *next;
};
main()
{
char num[10];
struct basic_meg *head;
while(1)
{
printf("--------------------------------\n");
printf("-----1 New calling card -----\n");
printf("-----2 List calling cards -----\n");
printf("-----3 Search calling card -----\n");
printf("-----4 Delete calling card -----\n");
printf("-----5 Insert calling card -----\n");
printf("-----0 Exit -----\n");
printf("--------------------------------\n");
printf("Please choice number:\n");
gets(num);
switch(*num)
{
case '1':head=New();break;
case '2':List(head);break;
case '3':Search();break;
case '4':head=Delete(head);break;
case '5':Insert();break;
case '0':break;
default:printf("Please enter a right number!\n");goto loop;
}
if(strcmp(num,"0")==0)
break;
}
return 0;
}
struct basic_meg *New(void)
{
int n;
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct basic_meg *) malloc(LEN);
printf("Please input name number and email.End of 0\n");
gets(p1->name);
gets(p1->number);
gets(p1->email);
head=NULL;
while(p1->name!=0)
{
n+=1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct basic_meg *)malloc(LEN);
printf("Please input name number and email.End of 0\n");
gets(p1->name);
gets(p1->number);
gets(p1->email);
printf("\n");
}
p2->next=NULL;
return(head);
}
void List(struct basic_meg *head)
{
struct basic_meg *p;
printf("\n these calling cards are :\n");
p=head;
if(head!=NULL)
do
{
puts(p->name);
puts(p->number);
puts(p->email);
printf("\n");
p=p->next;
}
while(p!=NULL);
}
void Search(struct basic_meg *head)
{
struct basic_meg *p1,*p2;
char name_search[20];
printf("Please input the name you want to search:\n");
gets(name_search);
if(head==NULL)
{
printf("\n list null!\n");
goto end;
}
p1=head;
while(name_search!=p1->name&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(name_search==p1->name)
{
if(p1==head)
head=p1->next;
else p2->next=p1->next;
printf("The basic message you search is:\n");
puts(name_search);
n=n-1;
}
else puts(name_search);
printf("is not found!\n");
return(head);
}
struct basic_meg *Delete(struct student *head)
{
struct basic_meg *p1,*p2;
char name_delete[20];
printf("Please input the name you want to delete:\n");
gets(name_delete);
if(head==NULL)
{
printf("\n list null!\n");
goto end;
}
p1=head;
while(name_delete!=p1->name&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(name_delete==p1->name)
{
if(p1==head)
head=p1->next;
else p2->next=p1->next;
printf("Successfully deleted!\n");
n=n-1;
}
else puts(name_delete);
printf("is not found!\n");
return(head);
}
struct basic_meg *Insert(struct basic_meg *head)
{
char name_insert[20];
struct basic_meg *p1,*p2,*p3;
p1=head;
printf("Please input the name where you want to insert:\n");
gets(name_insert);
if(head==NULL)
{
printf("\n list null!\n");
goto end;
}
while(name_insert!=p1->name&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(name_insert==p1->name)
{
p2=p1;
p3=p1->next;
p1=(struct student *) malloc(LEN);
printf("Please input name number and email.End of 0\n");
gets(p1->name);
gets(p1->number);
gets(p1->email);
p2->next=p1;
p1->next=p3;
}
else puts(name_insert);
printf("is not found!\n");
return(head);
}