BJFUOJ:基于链式存储结构的图书信息表的排序
代码
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int length = 0;
struct book{
string bookno;
string name;
double pride;
};
typedef struct Lnode{
book data;
struct Lnode *next;
}Lnode,*Linklist;
void Init_list(Linklist &l){
l = new Lnode;
l->next = NULL;
}
void input(Linklist &l){
string bookno;
string name;
double pride;
Linklist p,q;
p = l;
while(cin>>bookno>>name>>pride){
if(bookno == "0" && name == "0" && pride == 0) break;
q = new Lnode;
q->data.bookno = bookno;
q->data.name = name;
q->data.pride = pride;
q->next = NULL;
p->next = q;
p = q;
length++;
}
}
void sort(Linklist &l){
Linklist p,q;
p = l->next;
int j;
for(int i = 1; i < length; i++){
j = length - i;
p = l->next;
while(j--){
if(p->data.pride < p->next->data.pride){
swap(p->data,p->next->data);
}
p = p->next;
}
}
}
void output(Linklist &l){
Linklist q = l->next;
while(q){
cout<<q->data.bookno<<" "<<q->data.name<<" ";
printf("%.2lf\n",q->data.pride);
q = q->next;
}
}
int main(){
Linklist l;
Init_list(l);
input(l);
sort(l);
output(l);
return 0;
}