求大神解答(急) 链表相关 到底哪里写错了 总是输出有问题

问题描述:

图片说明

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
typedef struct _data
{
    char name[50];
    int age;
    char sex;
    char disease[50]; 
    int num;
}Data;
typedef struct _Node
{
    Data user;
    struct _Node*next;
}Node;
Node*create()
{
    Node*head=(Node*)malloc(sizeof(Node));
    if(head==NULL)
    exit (-1);
    head->next=NULL;
    return head;
}
void insert(Node*head,Data* per)
{
    Node*cur=(Node*)malloc(sizeof(Node));
    if(cur==NULL) exit(-1);

    head->user.age=per->age;
    head->user.sex=per->sex;
    head->user.num=per->num;
    strcpy(per->name ,head->user.name);
    strcpy(per->disease,head->user.disease);

    cur->next=head->next;
    head->next=cur;
}
int len(Node*head)
{
    int n=0;
    head=head->next;
    while(head)
    {
        n++;
        head=head->next;
    }
    return n;
}
void traverse(Node*head)
{
    head=head->next;
    while(head)
    {
        printf("%s %d %c %s %d\n",head->user.name,head->user.age,head->user.sex,head->user.disease,head->user.num);
        head=head->next;
    }
}
void pop(Node*head)
{
    int l=len(head);
    head=head->next;
    Node*p,*q;
    int m=0;
    for(int i=0;i<l-1;i++)
    {
        p=head;
        q=p->next;
        for(int j=0;j<l-1-i;j++)
        {
            if(p->user.num>q->user.num)
         {
            m=p->user.num;
            p->user.num=q->user.num;
            q->user.num=m;
         }
         p=p->next;
         q=p->next;
        }
    }
}
Node*search(Node*head,int num)
{
    head=head->next;
    while(head)
    {
        if(head->user.num==num)
          break;
        else
        head=head->next;
    }
    return head;
}
int main()
{
    printf("请输入病人信息包括:\n"); 
    printf("姓名  年龄  性别  病名  序号\n");
    Data per;
    Node*head=create();
    while(scanf("%s %d %c %s %d",per.name,&per.age,&per.sex,per.disease,&per.num)!=EOF )
    { 
        insert(head,&per);
    }   
    printf("打印病人人数\n");
    int mun=len(head);

    printf("按房间号排序\n");
    pop(head);
    traverse(head);

    printf("请输入待查找病人序号:\n");
    int n=0;
    scanf("%d",&n); 
    Node*find=search(head, n);
    if(find==NULL)
    printf("未查询到此人");
    else
    printf("查询成功");
    printf("%s %d %c %s %d",find->user.name,find->user.age,find->user.sex,find->user.disease,find->user.num) ;
    return 0;   
}

void insert(Node*head,Data* per) 的代码有问题
看我修改的代码

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
typedef struct _data
{
    char name[50];
    int age;
    char sex;
    char disease[50]; 
    int num;
}Data;
typedef struct _Node
{
    Data user;
    struct _Node*next;
}Node;
Node*create()
{
    Node*head=(Node*)malloc(sizeof(Node));
    if(head==NULL)
    exit (-1);
    head->next=NULL;
    return head;
}
void insert(Node*head,Data* per) 
{
    Node*cur=(Node*)malloc(sizeof(Node));
    if(cur==NULL) exit(-1);

    cur->user.age=per->age;
    cur->user.sex=per->sex;
    cur->user.num=per->num;
    strcpy(cur->user.name, per->name);
    strcpy(cur->user.disease, per->disease);

    cur->next=head->next;
    head->next=cur;
}
int len(Node*head)
{
    int n=0;
    head=head->next;
    while(head)
    {
        n++;
        head=head->next;
    }
    return n;
}
void traverse(Node*head)
{
    head=head->next;
    while(head)
    {
        printf("%s %d %c %s %d\n",head->user.name,head->user.age,head->user.sex,head->user.disease,head->user.num);
        head=head->next;
    }
}
void pop(Node*head)
{
    int l=len(head);
    head=head->next;
    Node*p,*q;
    int m=0;
    for(int i=0;i<l-1;i++)
    {
        p=head;
        q=p->next;
        for(int j=0;j<l-1-i;j++)
        {
            if(p->user.num>q->user.num)
         {
            m=p->user.num;
            p->user.num=q->user.num;
            q->user.num=m;
         }
         p=p->next;
         q=p->next;
        }
    }
}
Node*search(Node*head,int num)
{
    head=head->next;
    while(head)
    {
        if(head->user.num==num)
          break;
        else
        head=head->next;
    }
    return head;
}
int main()
{
    printf("请输入病人信息包括:\n"); 
    printf("姓名  年龄  性别  病名  序号\n");
    Data per;
    Node*head=create();
    while(scanf("%s %d %c %s %d",per.name,&per.age,&per.sex,per.disease,&per.num)!=EOF )
    { 
        insert(head,&per);
    }   
    printf("打印病人人数\n");
    int mun=len(head);

    printf("按房间号排序\n");
    pop(head);
    traverse(head);

    printf("请输入待查找病人序号:\n");
    int n=0;
    scanf("%d",&n); 
    Node*find=search(head, n);
    if(find==NULL)
    printf("未查询到此人");
    else
    printf("查询成功");
    printf("%s %d %c %s %d",find->user.name,find->user.age,find->user.sex,find->user.disease,find->user.num) ;
    return 0;   
}