建立一个单向链表,头指针是list,链表中每个结点包含姓名、基本工资信息,编写max_list函数查找链表中最高基本工资的职工信息。要求在主函数中建立单向链表(注:当输入基本工资为0时,表示输入结束。),然后调用max_list函数查找链表中最高基本工资的职工信息,最后输出查找结果。用C语言写
时间: 2023-05-24 22:02:44 浏览: 98
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Employee{
char name[20];
float salary;
struct Employee *next;
};
struct Employee *list;
void create_list(){
list = (struct Employee*)malloc(sizeof(struct Employee));
strcpy(list->name, "");
list->salary = 0;
list->next = NULL;
struct Employee *p = list;
struct Employee *newNode;
do{
newNode = (struct Employee*)malloc(sizeof(struct Employee));
printf("Please input the employee name: ");
scanf("%s", newNode->name);
printf("Please input the basic salary: ");
scanf("%f", &newNode->salary);
if(newNode->salary == 0)
break;
newNode->next = NULL;
p->next = newNode;
p = newNode;
}while(1);
}
void max_list(){
float maxsalary = 0;
struct Employee *p = list->next;
while(p != NULL){
if(p->salary > maxsalary)
maxsalary = p->salary;
p = p->next;
}
p = list->next;
while(p != NULL){
if(p->salary == maxsalary)
printf("The employee with max salary is: %s, salary: %f\n", p->name, p->salary);
p = p->next;
}
}
int main(){
create_list();
max_list();
return 0;
}
阅读全文