请问,为什么这个代码只能输入,没有输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct list{
int num;
char name[30];
double salary;
struct list*next;
};
struct list *create()
{
struct list *head;
struct list p;
struct list tail;
int n=0;
int num;
char name[30];
double salary;
//tail=p=(struct list)malloc(sizeof(struct list));
scanf("%d",&num);
while(num!=0){
n++;
scanf("%s\t%ld",name,&salary);
p=(struct list)malloc(sizeof(struct list));
p->num=num;p->salary=salary;strcpy(p->name,name);
p->next=NULL;
if(n==1)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
return head;
}
int main()
{
printf("请输入职工信息:");
struct list *head=create();
void max_list(struct list *head);
max_list(head);
return 0;
}
void max_list(struct list *head)
{
struct list *z,q;
q=z=(struct list)malloc(sizeof(struct list));
z=head;
for(q=head;q->next!=NULL;q=q->next)
{
if(q->salary>z->salary)
{
z=q;
}
z->next=NULL;
}
printf("最高基本工资的员工信息:&s &ld",z->name,z->salary);
//return head;
}
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct list {
int num;
char name[30];
double salary;
struct list* next;
};
struct list* create()
{
struct list* head;
struct list* p;
struct list* tail;
int n = 0;
int num;
char name[30];
double salary;
//tail=p=(struct list)malloc(sizeof(struct list));
scanf("%d", &num);
while (num != 0) {
n++;
scanf("%s %ld", name, &salary); //scanf("%s\t%ld", name, &salary); 修改
p = (struct list*)malloc(sizeof(struct list));
p->num = num;
p->salary = salary;
strcpy(p->name, name);
p->next = NULL;
if (n == 1)
head = p;
else
tail->next = p;
tail = p;
scanf("%d", &num);
}
return head;
}
int main()
{
printf("请输入职工信息:");
struct list* head = create();
void max_list(struct list* head);
max_list(head);
return 0;
}
void max_list(struct list* head)
{
struct list* z,* q;
//q = z = (struct list*)malloc(sizeof(struct list)); //修改
z = head;
for (q = head; q != NULL; q = q->next)
//for (q = head; q->next != NULL; q = q->next)修改
{
if (q->salary > z->salary)
{
z = q;
}
//z->next = NULL; 修改
}
printf("最高基本工资的员工信息:%s %ld", z->name, z->salary);
//printf("最高基本工资的员工信息:&s &ld",z->name,z->salary); 修改
//return head;
}
请问你的代码是可以直接运行么,不会出现 不允许强制转换到类型 "struct list" 么
这里应该要转换成struct list*
类型。
粘贴代码的时候麻烦粘贴完整的代码块,而不是一半有一半没有
1、在你的main函数中,在struct list *head=create();后面吧你的head打印出来,看head是否得到了数据;
2、在你的max_list 函数中的for 怎加打印信息,看看在这个函数中哪里出错了。