数据结构与算法-27基于链式存储结构的图书信息表的逆序存储

Description
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后将读入的图书逆序存储,逐行输出逆序存储后每本图书的信息。

Input
输入n+1行,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。

Output
总计n行,第i行是原有图书表中第n-i+1行的图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔。其中价格输出保留两位小数。

Sample Input

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

Sample Output

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

参考程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 100
#define ROOM sizeof(struct BookInfo)

struct BookInfo
{
	char Number[LEN];
	char Name[LEN];
	float Price;
	struct BookInfo *next;
};

struct BookInfo* CreatList(int n)
{//头插法创建链表 
	int i;
	struct BookInfo *head,*p1;
	head=(struct BookInfo*)malloc(ROOM);
	head->next=NULL;
	for(i=0;i<n;i++)
	{
		p1=(struct BookInfo*)malloc(ROOM);
		scanf("%s%s%f",p1->Number,p1->Name,&p1->Price);
		p1->next=head->next;
		head->next=p1;
	}
	return head;
}


int main()
{
	int n;
	struct BookInfo *List,*p1;
	scanf("%d",&n);
	List=CreatList(n);
	p1=List->next;
	while(p1)
	{//遍历链表 
		printf("%s %s %.2f\n",p1->Number,p1->Name,p1->Price);
		p1=p1->next;
	}
	return 0;
}

/*
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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值