基于链式存储结构的图书信息表的旧图书的出库
描述
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的待出库的旧图书的位置,将该图书从图书表中删除,最后输出该图书出库后的所有图书的信息。
输入
总计n+2行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。之后输入第n+2行,内容仅为一个整数,代表待删除的旧图书的位置序号。
输出
若删除成功: 输出旧图书出库后所有图书的信息(书号、书名、价格),总计n-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 2
输出样例 1
9787302257646 Data-Structure 35.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
输入样例 2
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 9
输出样例 2
Sorry,the position to be deleted is invalid!
#include<iostream>
#include<string>
#include<iomanip>
#define Maxsize 100
using namespace std;
struct Book {
string no;
string name;
double price;
};
typedef struct LNode {
Book data;
struct LNode* next;
}LNode, *LinkList;
void InitLinkList(LinkList &L) {
L = new LNode;
L->next = NULL;
}
LinkList TailInsert(LinkList& L) {
Book a;
LNode* s,*r=L;
int n;
cin >> n;
while (n!=0)
{
cin >> a.no >> a.name >> a.price;
s = new LNode;
s->data = a;
r->next = s;
r= s;
n--;
}
r->next = NULL;
return L;
}
void output(LinkList& L) {
int no;
cin >> no;
int n = 0;
LNode* p = L->next;
while (p != NULL) {
n++;
p = p->next;
}
if (no <= 0 || no > n ) {
cout << "Sorry,the position to be deleted is invalid!" << endl;
}
else {
p = L->next;
int k = 0;
while (p != NULL) {
k++;
if (no == k + 1) {
LNode* s = new LNode;
s = p->next;
p ->next= p->next->next;
free(s);
continue;
}
p = p->next;
}
p = L->next;
while (p != NULL) {
cout << p->data.no << " " << p->data.name << " " << fixed << setprecision(2) << p->data.price << endl;
p = p->next;
}
}
}
int main() {
LinkList L;
InitLinkList(L);
TailInsert(L);
output(L);
return 0;
}