数据结构C++,在里面在写一个排序的算法,最好是冒泡排序

#include<iostream>
using namespace std;
#define NAMELEN 20
#define SEXLEN 7
#define CODELEN 10
struct node
{
char name[NAMELEN+1];
char num[ CODELEN+1];
char sex[SEXLEN+1];
int chinese;
int english;
int math;
node *next;
};
class student
{
public:
node* creat();
void display();
void aver();
void addperson();
void denode();
void find();
void chosse();
void help();
void print();
public:
node* head;
};
class senior1:public student
{
} ;
class senior2:public student
{
};
node* student::creat()
{
node *p,*rear;
char c;
head=new node;
rear=head;
while(1)
{
p=new node;
cout<<"请输入学生的姓名:"<<endl;
cin>>p->name;
cout<<"请输入他的学号:"<<endl;
cin>>p->num;
cout<<"请输入性别 :"<<endl;
cin>>p->sex;
cout<<"请输入语文成绩:" <<endl;
cin>>p->chinese;
cout<<"请输入英语成绩:"<<endl;
cin>>p->english;
cout<<"请输入数学" <<endl;
cin>>p->math;
rear->next=p;
rear=p;
cout<<"时候继续录入学生信息:n/y?"<<endl;
cin>>c;
if(c=='n')
break;
}
rear->next=NULL;
return head;
}
void student::display()
{
node *s;
s=head->next;
cout<<"姓名"<<ends<<"学号"<<ends<<"性别"<<ends<<"语文"<<ends<<"英语"<<ends<<"数学"<<endl;
while(s)
{
cout<<s->name<<ends<<s->num<<ends<<s->sex<<ends<<s->chinese<<ends<<s->english<<ends<<s->math<<ends<<endl;
s=s->next;
}
}
void student::aver()
{
node *p;int i=0;
p=head->next;
double ave1=0,ave2=0,ave3=0;
while(p!=NULL)
{
ave1+=p->chinese;
ave2+=p->english;
ave3+=p->math;
p=p->next;i++;
}
cout<<"语文平均成绩为"<<ave1/i<<endl;
cout<<"英语平均成绩为"<<ave2/i<<endl;
cout<<"数学平均成绩为"<<ave3/i<<endl;
}

#include <iostream>
using namespace std;
#define NAMELEN 20
#define SEXLEN 7
#define CODELEN 10
struct node {
char name[NAMELEN + 1];
char num[CODELEN + 1];
char sex[SEXLEN +1];
int chinese;
int english;
int math;
node *next;
};

class student {
public:
node *creat();
void display();
void aver();
void addperson();
void denode();
void find();
void chosse();
void help();
void print();
void sort(); // 添加排序函数
public:
node* head;
};

class senior1:public student{};class senior2:public student{};
node* student::creat() {
node *p,*rear;
char c;
head = new node;
rear = head;
while(1) {
p = new node;
cout << "请输入学生的姓名 : ";
cin >> p->name;
cout << "请输入他的学号 : ";
cin >> p->num;
cout << "请输入性别 : ";
cin >> p->sex;
cout << "请输入语文成绩 : ";
cin >> p->chinese;
cout << "请输入英语成绩 : ";
cin >> p->english;
cout << "请输入数学成绩 : ";
cin >> p->math;
rear->next = p;
rear = p;
cout << "时候继续录入学生信息(n/y) : ";
cin >> c;
if(c == 'n') break;
}
rear->next = NULL;
return head;
}

// 根据chinese成绩降排序
void student::sort() {
node *q,*qt,*p,*pt;
p = head;
while(p->next) {
q = p->next;
while(q->next) {
if(p->next->chinese < q->next->chinese) {
pt = p->next;
qt = q->next;
q->next = qt->next;
p->next = qt;
qt->next = pt;
}
else q = q->next;
}
p = p->next;
}
}追问

我在后面加了输出,但是为什么执行起来有错?名字不能这样直接显示出来么?
if(p->next->chinese next->chinese) {
pt = p->next;
qt = q->next;
q->next = qt->next;
p->next = qt;
qt->next = pt;
}
else q = q->next;
coutnext->chinesenext;

追答

sort()是排序函数,需要显示调用,若希望显示数据,则应添加显示函数。
void show() {
node *p;
for(p = head->next; p ; p = p->next)
cout chinese english math << endl;
}

追问

我在后面的主函数有调用的语句,但是我想直接在这里输出成绩然后后面显示成绩,我这样这能显示出成绩的排序,但是名字对不上而且还有乱码

追答

不要在排序函数中显示,因为显示顺序是未知的,所以效果可想而知,建议在排序前,或排序后再显示。

追问

您能给我一个邮箱么,我这里还有一个程序,步骤比较多写不下,我发给您,有问题向您请教

追答

[email protected]

追问

已经发送了,您看下

追答

看不成.docx,只能看.doc

温馨提示:答案为网友推荐,仅供参考