"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 09:05:37 浏览: 75
结构体类型代码1.zip
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu{
char name[20];
float price;
struct stu *next;
};
void createList(struct stu **head) {
char c;
struct stu *tail = *head;
do {
struct stu *node = (struct stu *)malloc(sizeof(struct stu));
printf("请输入书名价格:\n");
scanf("%s%f", node->name, &(node->price));
node->next = NULL;
if (*head == NULL) {
*head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
scanf(" %c", &c);
} while (c == 'Y' || c == 'y');
}
void freeList(struct stu *head) {
while (head != NULL) {
struct stu *temp = head->next;
free(head);
head = temp;
}
}
void printMax(struct stu *head) {
if (head == NULL) {
printf("链表为空\n");
return;
}
struct stu *maxNode = head;
while (head != NULL) {
if (head->price > maxNode->price) {
maxNode = head;
}
head = head->next;
}
printf("result:\n%s %.2f\n", maxNode->name, maxNode->price);
}
int main() {
struct stu *head = NULL;
createList(&head);
printMax(head);
freeList(head);
return 0;
}
```
阅读全文