描述
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后查找价格最高的图书,输出相应图书的信息。
输入
总计输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计输出m+1行,其中,第一行是最贵图书数目(价格最高的图书可能有多本),后m行是最贵图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,其中价格输出保留两位小数。
#include<iostream>
#include<iomanip>
using namespace std;
typedef struct book {
string id;
string name;
float money;
};
typedef struct node {
book data;
struct node* next;
}node, * linklist;
void innit(linklist& list) {
list = new node;//分配大小为node的空间
list->next = NULL;
list->data.money = 0;//头结点存放链表长度;
}
//尾插法构建单链表
void creat_linklist(linklist& list) {
node* p = NULL;//插入节点
string a, b;
float c;
int length = 0;
node* r;
r = new node;
r = list;
while (length < list->data.money) {
cin >> a >> b >> c;
p = new node;//分配新空间
p->data.id = a;
p->data.name = b;
p->data.money = c;//赋值
r->next = p;//尾插法
r=r->next;
length++;//链表长度加一
//cin >> a >> b >> c;
}
r->next = NULL;
p= NULL;//一定要先置空,再回收指针
delete p;//回收指针
}
//输出
void print(linklist list) {
node* p;
p = list;
while (p->next != NULL) {
p = p->next;
cout << p->data.id << " " << p->data.name << " " << fixed << setprecision(2) << p->data.money << endl;
}
p = NULL;
delete p;
}
//按价格降序-直接插入法
void jiangxu(linklist& list) {
node* p = NULL;//正待排序结点
p = new node;
p = list->next;
node* r = NULL;//在有序序列中查找 待排序结点的位置
node* f = NULL;
f = new node;
r = new node;
if (p->next != NULL)//链表不只有一个结点
p = p->next;//第二个结点
list->next->next = NULL;//已有序队列尾端置空
while (p!= NULL) {
//p = p->next;
float inserting = p->data.money;//待插入图书价格
r = list->next;//搜索指针前指针
f = list;//搜索指针后指针
node* q = NULL;//临时指针
q = new node;
q = p;
p = p->next;//下一个待插图书信息
while (1) {
float inserted = r->data.money;
if (inserting > inserted) {
//头插,在inserted 前面
q->next = r;
f->next = q;
break;
}
if (inserting < inserted && r->next != NULL) { r = r->next; f = f->next; continue; }
if (inserting == inserted ||(r->next == NULL && inserting < inserted)) {//相等or最小,后插
q->next = r->next; r->next = q;
//cout << q->data.money << endl;
break;
}
}
}
}
void expensive_book(linklist& list,linklist &expense) {
node* p,*r;//搜索最贵图书
node* exp;//构造最贵图书链表
int num_exp = 0;//最贵图书的个数
exp = new node;
p = new node;
r = new node;
jiangxu(list);//降序排列
p = list->next;//p初始指向结点1
r = list->next->next;//r初始指向结点2
exp = expense;//指向头结点
node* data;
if (p != NULL) {
data = new node;
*data = *p;//复制节点
exp->next = data;
exp = exp->next;
//expense[num_exp++]=*p;
num_exp++;
while (r != NULL && r->data.money == p->data.money) {
data = new node;
*data = *r;
exp->next = data;
exp = exp->next;
p = p->next;
r = r->next;//指针一起后移
num_exp++;
break;
}
exp->next = NULL;
expense->data.money = num_exp;//把最贵图书个数存放于头结点中
}
else{ expense->data.money = num_exp; }
}
int main() {
linklist list;//图书信息表
linklist expense;//最贵图书信息表
list = new node;
int length;//图书信息表 表长
innit(list);//初始化链表
innit(expense);//初始化最贵图书链表
cin >> length;
list->data.money = length;//将表长存放于头结点中
creat_linklist(list);//创建单链表
expensive_book(list,expense);
cout << expense->data.money<< endl;
print(expense);
//print(list);//测试原链表未被破坏
}