#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;
}
求大神解答(急) 链表相关 到底哪里写错了 总是输出有问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2020-08-19 08:49关注
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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 #MATLAB仿真#车辆换道路径规划
- ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
- ¥15 数据可视化Python
- ¥15 要给毕业设计添加扫码登录的功能!!有偿
- ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
- ¥15 微信公众号自制会员卡没有收款渠道啊
- ¥100 Jenkins自动化部署—悬赏100元
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘