基于链式存储结构的图书信息表的输出C语言代码
时间: 2024-03-26 16:30:31 浏览: 68
链式存储结构用二叉链表即可方便表示。-C语言数据结构课件【比较清晰】
以下是基于链式存储结构的图书信息表的输出C语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book {
char id[20];
char name[50];
float price;
struct book *next;
} Book;
int main() {
int n = 0;
float sum = 0, avg = 0;
Book *head = NULL, *p = NULL, *q = NULL;
// 输入图书信息
while (1) {
p = (Book *)malloc(sizeof(Book));
scanf("%s", p->id);
if (strcmp(p->id, "0") == 0) {
free(p);
break;
}
scanf("%s%f", p->name, &p->price);
sum += p->price;
n++;
if (head == NULL) {
head = p;
} else {
q->next = p;
}
q = p;
}
// 计算平均价格
avg = sum / n;
// 修改价格并输出图书信息
p = head;
while (p != NULL) {
if (p->price < avg) {
p->price *= 1.2;
} else {
p->price *= 1.1;
}
printf("%s %s %.2f\n", p->id, p->name, p->price);
p = p->next;
}
// 释放链表内存
p = head;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
return 0;
}
```
阅读全文