基于链式存储结构的图书信息表的最佳位置图书的查找C语言代码
时间: 2023-12-19 13:07:02 浏览: 100
基于链式存储结构的图书信息表的最佳位置图书的查找C语言代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 50
typedef struct book {
char id[MAXLEN];
char name[MAXLEN];
float price;
struct book *next;
} Book;
Book *create_book_list(int n) {
Book *head, *p, *q;
int i;
head = (Book *)malloc(sizeof(Book));
head->next = NULL;
q = head;
for (i = 0; i < n; i++) {
p = (Book *)malloc(sizeof(Book));
scanf("%s%s%f", p->id, p->name, &p->price);
p->next = NULL;
q->next = p;
q = p;
}
return head;
}
void print_book_list(Book *head) {
Book *p;
p = head->next;
while (p != NULL) {
printf("%s %s %.2f\n", p->id, p->name, p->price);
p = p->next;
}
}
Book *find_book_by_index(Book *head, int index) {
Book *p;
int i;
p = head->next;
for (i = 1; i < index && p != NULL; i++) {
p = p->next;
}
return p;
}
int main() {
int n, m, i, index;
Book *head, *p;
scanf("%d", &n);
head = create_book_list(n);
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%d", &index);
p = find_book_by_index(head, index);
if (p != NULL) {
printf("%s %s %.2f\n", p->id, p->name, p->price);
}
}
return 0;
}
```
阅读全文