数据结构C++,在里面在写一个排序的算法,最好是冒泡排序
发布网友
发布时间:2022-07-19 15:41
我来回答
共1个回答
热心网友
时间:2023-10-16 14:37
#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;
}
}