BJFU 214基于链式存储结构的图书信息表的创建和输出

#include<bits/stdc++.h>
using namespace std;

struct Book {
	string bookno;
	string name;
	double price;
};

//结点
typedef struct Lnode {
	Book data;
   struct Lnode *next;
}Lnode,*Linklist;

//初始化
void Initlist(Linklist &L) {
	L = new Lnode;
	L->next = NULL;
}

//尾插建表
void Creatlist(Linklist &L, string bookno, string name, double price) {
	Lnode *p = new Lnode;
	p->data.bookno = bookno;
	p->data.name = name;
	p->data.price = price;
	p->next = NULL;       //处理新节点

	Lnode *cur = L;
	while (cur->next)
	{
		cur = cur->next;
	}
	cur->next = p;
	p->next = NULL;
}

//打印
void Print(Linklist &L) {
	Lnode *cur = L->next;
	while (cur != NULL) {
		cout << cur->data.bookno << " " << cur->data.name << " ";
		printf("%.2lf\n", cur->data.price);
		cur = cur->next;
	}
}

int main() {
	string bookno;
	string name;
	double price;
	int count = 0;  //统计个数

	Linklist L;
	Initlist(L);
	while (cin>>bookno>>name>>price)
	{
		if (bookno == "0"&&name == "0"&&price == 0) 
			break; //退出条件
		count++;
		Creatlist(L, bookno, name, price);  //建图书表
	}
	printf("%d\n", count);
	Print(L);
}