c++ 链表快速排序

来源:百度知道 编辑:UC知道 时间:2024/07/06 16:23:48
typedef struct LStudent {
string id;
string name;
float math;
float chinese;
float english;
float aver;
} Str_Stu;

typedef struct LNode {
Str_Stu data;
struct LNode *next;
}LNode, *pLinkList;

class student {
private:
pLinkList m_pList;
int m_listLength;
………………
有上面这样一个结构体,以及类,类中的数据按aver有序排列。现在要给这个类写一个函数,让它按math或者chinese或者english从高到低有序显示,使用快速排序的话要怎么操作呢?(注,有序显示的时候类中的数据顺序不变,仍然按aver有序)
我的想法是,再别设一个链表来保存排序后的数据。

参考代码,由于没有测试过,可能存在错误。

#include <iostream>
using namespace std;

typedef struct LStudent {
string id;
string name;
float math;
float chinese;
float english;
float aver;
} Str_Stu;

typedef struct LNode {
Str_Stu data;
struct LNode *next;
}LNode, *pLinkList;

enum SortType {__Math = 0, __Chinese, __English};

class student {
// 为了方便编写代码先把成员变量设置为public
public:
pLinkList m_pList;
int m_listLength;
};

inline void swap(Str_Stu **pp, int k, int j)
{
Str_Stu *temp;
temp = pp[k];
pp[k] = pp[j];
pp[j] = temp;
}

void qsort(Str_Stu **pp, int left, int right, SortType st)
{
int j, last;

if (left >= right)
return;

swap(pp, left, (left + right)/2);
last=left;

for (j = left+1; j <= right; j++)
{
if (st == __Math && pp[