基于顺序存储结构的图书信息表的逆序存储代码
时间: 2023-05-11 16:07:08 浏览: 200
下面是基于顺序存储结构的图书信息表的逆序存储代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct {
char name[20];
char author[20];
char publisher[20];
float price;
} Book;
typedef struct {
Book data[MAXSIZE];
int length;
} BookList;
void reverse(BookList *L) {
int i;
Book temp;
for (i = 0; i < L->length / 2; i++) {
temp = L->data[i];
L->data[i] = L->data[L->length - i - 1];
L->data[L->length - i - 1] = temp;
}
}
int main() {
BookList L;
int i;
L.length = 5;
strcpy(L.data[0].name, "C Programming");
strcpy(L.data[0].author, "Dennis Ritchie");
strcpy(L.data[0].publisher, "Prentice Hall");
L.data[0].price = 22.99;
strcpy(L.data[1].name, "The C Programming Language");
strcpy(L.data[1].author, "Brian Kernighan");
strcpy(L.data[1].publisher, "Prentice Hall");
L.data[1].price = 31.55;
strcpy(L.data[2].name, "Head First C");
strcpy(L.data[2].author, "David Griffiths");
strcpy(L.data[2].publisher, "O'Reilly Media");
L.data[2].price = 29.99;
strcpy(L.data[3].name, "C Primer Plus");
strcpy(L.data[3].author, "Stephen Prata");
strcpy(L.data[3].publisher, "Sams Publishing");
L.data[3].price = 39.99;
strcpy(L.data[4].name, "Effective C");
strcpy(L.data[4].author, "Scott Meyers");
strcpy(L.data[4].publisher, "Addison-Wesley Professional");
L.data[4].price = 49.99;
printf("Original book list:\n");
for (i = 0; i < L.length; i++) {
printf("%s\t%s\t%s\t%.2f\n", L.data[i].name, L.data[i].author, L.data[i].publisher, L.data[i].price);
}
reverse(&L);
printf("Reversed book list:\n");
for (i = 0; i < L.length; i++) {
printf("%s\t%s\t%s\t%.2f\n", L.data[i].name, L.data[i].author, L.data[i].publisher, L.data[i].price);
}
return 0;
}
```
希望能够帮到你!
阅读全文