基于链式存储结构的图书信息表的逆序存储
描述
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后将读入的图书逆序存储,逐行输出逆序存储后每本图书的信息。
输入
输入n+1行,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计n行,第i行是原有图书表中第n-i+1行的图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔。其中价格输出保留两位小数。
输入样例 1
8 9787302257646 Data-Structure 35.00 9787302164340 Operating-System 50.00 9787302219972 Software-Engineer 32.00 9787302203513 Database-Principles 36.00 9787810827430 Discrete-Mathematics 36.00 9787302257800 Data-Structure 62.00 9787811234923 Compiler-Principles 62.00 9787822234110 The-C-Programming-Language 38.00
输出样例 1
9787822234110 The-C-Programming-Language 38.00 9787811234923 Compiler-Principles 62.00 9787302257800 Data-Structure 62.00 9787810827430 Discrete-Mathematics 36.00 9787302203513 Database-Principles 36.00 9787302219972 Software-Engineer 32.00 9787302164340 Operating-System 50.00 9787302257646 Data-Structure 35.00
来源
BJFU
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Book
{
char num[20];
char name[50];
float price;
};
struct Libra
{
struct Book elem;
struct Libra *next;
};
int main()
{
struct Libra *head,*current;
int length=0;
head = (struct Libra*)malloc(sizeof(struct Libra));
head->next = NULL;
scanf("%d",&length);
for(int i=0;i<length;i++)
{
current = (struct Libra*)malloc(sizeof(struct Libra));
scanf("%s %s %f",current->elem.num,current->elem.name,¤t->elem.price);
current->next = head->next;
head->next = current;
}
current = head;
while(current->next != NULL)
{
printf("%s %s %.2f\n",current->next->elem.num,current->next->elem.name,current->next->elem.price);
current=current->next;
}
return 0;
}