描述
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后将读入的图书逆序存储,逐行输出逆序存储后每本图书的信息。
输入
输入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
查看隐藏信息
本题最开始没有理解题意,导致我先顺序存储,在进行逆序存储。
没有任何思路,之后看了题解,发现原来最开始就是逆序存储。
然后有一个优化,就是在循环里面定义LNode *p=new LNode。
然后其他都还好。
代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define OK 1
using namespace std;
typedef struct
{
char no[20];
char name[50];
float price;
}Book;
typedef struct LNode
{
Book data;
struct LNode *next;
}LNode,*LinkList;
LinkList L;
bool InintList(LinkList &L)
{
L=new LNode;
L->next=NULL;
return OK;
}
int main()
{
int num;
InintList(L);
L->next=NULL;
cin>>num;
for(int i=0;i<num;i++)
{
LNode *p=new LNode;
scanf("%s%s%f",p->data.no,p->data.name,&p->data.price);
p->next=L->next;
L->next=p;
}
LNode *it=L->next;
while(it)
{
printf("%s %s %.2f\n",it->data.no,it->data.name,it->data.price);
it=it->next;
}
return 0;
}