链表输入和打印出现乱码

代码如下:


#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define l sizeof(struct friends)

int i,n;char c,ch[20]={0};

struct friends
{
    char name[20];
    char phone[12];
    struct friends*next;
};

struct friends *create()
{
    struct friends *head;
    struct friends *p1,*p2;
    n=0;
    p1=p2=(struct friends*)malloc(l);
    do
    {
        printf("请依次输入每个联系人姓名、电话(空格间隔):");
        scanf("%s,%s",&p2->name,&p2->phone);
        if(n==0){head=p2;head->next=p1;}
        else {p1=p2;p1->next=p2;}
        p2=(struct friends*)malloc(l);
        printf("是否继续输入,按y继续输入,其他键结束:");
        scanf("%c",&c);
        n++;
    }while(c=='y');
    p2->next=NULL;
    return(head);
}

void print(struct friends *head)
{
    for(i=0;i<n;i++)
    {
        printf("%s     %s\n",head->name,head->phone);
        head=head->next;
    }
}

void search(struct friends *head)
{
    printf("请输入要查找联系人姓名:\n");
    scanf("%s",&ch);
    for(i=0;i<n;i++)
    {
        if(head->name==ch){printf("该联系人的姓名: %s 电话: %s",head->name,head->phone);break;}
        head=head->next;
    }
}

void destory(struct friends *head)
{
    struct friends *p;
    while(head->next)
    {
    p=head;
    head=head->next;
    free(p);
    }
}

int main()
{
    struct friends *pt;
    pt=create();
    print(pt);
    search(pt);
    destory(pt);
}

预期结果如下:

img

实际输出如下:

img

不知道是哪里出了问题,希望有人能答疑解惑,非常感谢!

主要有这几个问题,一是输入与scanf的说明符不符合,正确的应该形如(xu,15645765),二是结构指针的赋值有点问题,最后是给结构分配内存的问题,下面是一个实现:

 
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#define l sizeof(struct friends)
int i,n;char c,ch[20]={0};
struct friends
{
    char name[20];
    char phone[12];
    struct friends*next;
};
struct friends *create()
{
    struct friends *head;
    struct friends *p1,*p2;
    n=0;
    
    p2=p1=(struct friends*)malloc(sizeof (struct friends));
    
    do
    {
        printf("请依次输入每个联系人姓名、电话(空格间隔):");
        if(p1==NULL){
            p1=(struct friends*)malloc(sizeof (struct friends));
        }
      //  printf("\np1=%d\n",p1);
        scanf("%s,%s",&p1->name,&p1->phone);
      //  printf("&p1->name=%s,&p1->phone=%s\n",p1->name,p1->phone);
        if(n==0){
        //    printf("n==0\n");
            head=p1;
            head->next=NULL;
        }
        else {
        //    printf("n=%d\n",n);
            head->next=p1; 
            head=head->next;
        }
        while(getchar()!='\n')
            continue;
        printf("是否继续输入,按y继续输入,其他键结束:");
        scanf("%c",&c);
        n++;
     //   printf("p1=%d,head=%d,head.next=%d\n",p1,head,head->next);
        p1=NULL;
    }while(c=='y');
    head->next=NULL;
    //printf("n=%d\n",n);
    return(p2);
}
void print(struct friends *head)
{
    for(i=0;i<n;i++)
    {
        printf("%s     %s\n",head->name,head->phone);
       // printf("head=%d,head->next=%d\n",head,head->next);
        head=head->next;
       // printf("head=%d,head->next=%d\n",head,head->next);
    }
}
void search(struct friends *head)
{
    printf("请输入要查找联系人姓名:\n");
    scanf("%s",ch);
   // printf("姓名是:%s\n",ch);
    for(i=0;i<n;i++)
    {
        if(strcmp(head->name,ch)==0){
            printf("该联系人的姓名: %s 电话: %s",head->name,head->phone);
            break;
        }
        head=head->next;
    }
}
void destory(struct friends *head)
{
    struct friends *p;
    while(head->next)
    {
    p=head;
    head=head->next;
    free(p);
    }
}
int main()
{
    struct friends *pt;
    pt=create();
    print(pt);
    search(pt);
    destory(pt);
}