首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言通讯录,求大神帮忙查错!解决方案

2013-06-26 
C语言通讯录,求大神帮忙查错!#includestdio.h#includestring.h#includestdlib.h#define LEN sizeof(

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 Newcalling 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); 
}
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 Newcalling 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);
loop:gets(num);
switch(*num)
{
case '1':head=New();break;
case '2':List(head);break;
//case '3':Search();break;
case '3':Search(head);break;
case '4':head=Delete(head);break;
//case '5':Insert();break;
case '5':Insert(head);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 basic_meg *head;
//struct student  *p1,*p2; 
struct basic_meg *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];
int n = 0;
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");
end:return; 
}

struct basic_meg *Delete(struct basic_meg *head)

struct basic_meg *p1,*p2;
char name_delete[20];
int n = 0;
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");
end: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 basic_meg *) 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");
end:return(head); 
}

热点排行