"1.(16分)输出最贵的书籍信息:结构体类型定义如下:struct stu{char name[20];float price;struct stu *next;} ;要求:(1)编写自定义函数创建链表,输入书名和价格。输入Y继续创建,输入其他字符结束创建。(2)编写自定义函数输出最贵的书籍信息。(3)编写自定义函数释放链表内存。**输入提示:\"请输入书名价格:\\n\"(循环)**输入格式:\"%s %f\"**是否继续创建的提示:\"是否继续输入,按Y键继续输入,其他键就结束.\\n\"**输出提示:\"result:\\n\"**输出格式:\"%s %.2f\\n\"程序运行示例:请输入书名价格:Algorithms 105是否继续输入,按Y键继续输入,其他键就结束.Y请输入书名价格:高等数学31.5是否继续输入,按Y键继续输入,其他键就结束.Y请输入书名价格:C语言35是否继续输入,按Y键继续输入,其他键就结束.nresult:67 ----+ 1mm- 1 0K 00.-"
时间: 2023-07-07 08:05:40 浏览: 33
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu {
char name[20];
float price;
struct stu *next;
};
struct stu* create_list() {
struct stu *head = NULL, *p, *tail = NULL;
char choice;
do {
p = (struct stu*)malloc(sizeof(struct stu));
printf("请输入书名价格:\n");
scanf("%s %f", p->name, &p->price);
p->next = NULL;
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
getchar();
choice = getchar();
} while (choice == 'Y' || choice == 'y');
return head;
}
void print_max(struct stu *head) {
if (head == NULL) {
printf("链表为空!\n");
return;
}
struct stu *p = head;
float max_price = p->price;
char max_name[20];
while (p != NULL) {
if (p->price > max_price) {
max_price = p->price;
strcpy(max_name, p->name);
}
p = p->next;
}
printf("result:\n");
printf("%s %.2f\n", max_name, max_price);
}
void free_list(struct stu *head) {
struct stu *p = head, *q;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
}
int main() {
struct stu *head = create_list();
print_max(head);
free_list(head);
return 0;
}
```
运行示例:
```
请输入书名价格:
Algorithms 105
是否继续输入,按Y键继续输入,其他键就结束.
y
请输入书名价格:
高等数学 31.5
是否继续输入,按Y键继续输入,其他键就结束.
y
请输入书名价格:
C语言 35
是否继续输入,按Y键继续输入,其他键就结束.
n
result:
Algorithms 105.00
```