对于非内部数据类型的对象(在这里指的是string)而言,
光用maloc/free无法满足动态对象的要求。
对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数。
由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,
不能够把执行构造函数和析构函数的任务强加于malloc/free。
测试文件:
光用maloc/free无法满足动态对象的要求。
对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数。
由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,
不能够把执行构造函数和析构函数的任务强加于malloc/free。
所以这里(详情见实现文件)(要使用new,而不是malloc
头文件:
#ifndef QUICKSORT_H
#define QUICKSORT_H
#include <string>
#define MaxSize 100;
using std::string;
struct MyStudent
{
long num;
string name;
int grade;
};
typedef struct MyStudent Student;
struct MyList
{
Student * data;
int length;
};
typedef struct MyList List;
void CreatList(List & L, int size);//创建空顺序表
void AddLast(List & L, long _num, string _name, int _grade);//在表尾添加数据
void Exchange(List & L, int index_front, int index_last);//交换指定下标的数据域
int Partition(List & L, int low, int high);//对下标为low与high之间的数据进行一趟排序
void QuickSort(List & L, int low, int high);//完成快排
void PrintList(List L);//按顺序输出顺序表中数据
#endif
#include "QuickSort.h"
#include <iostream>
#include <string>
using namespace std;
void CreatList(List & L, int size)
{
L.data = new Student[size];
/*
对于非内部数据类型的对象(在这里指的是string)而言,
光用maloc/free无法满足动态对象的要求。
对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数。
由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,
不能够把执行构造函数和析构函数的任务强加于malloc/free。
所以这里要使用new,而不是malloc
*/
L.length = 0;
}
void AddLast(List & L, long _num, string _name, int _grade)
{
if (L.length > 100){
exit(-1);
}
L.data[L.length].num = _num;
L.data[L.length].name = _name;
L.data[L.length].grade = _grade;
L.length++;
}
static void Exchange(List & L, int index_front, int index_last)
{
Student tempStudent;
tempStudent.num = L.data[index_front].num;
tempStudent.name = L.data[index_front].name;
tempStudent.grade = L.data[index_front].grade;
L.data[index_front].num = L.data[index_last].num;
L.data[index_front].name = L.data[index_last].name;
L.data[index_front].grade = L.data[index_last].grade;
L.data[index_last].num = tempStudent.num;
L.data[index_last].name = tempStudent.name;
L.data[index_last].grade = tempStudent.grade;
}
static int Partition(List & L, int low, int high)
{
int key = low;//key值为low
while (low < high){
while (low < high && L.data[high].grade <= L.data[key].grade){
high--;
}
Exchange(L, key, high);
key = high;
while (low < high && L.data[low].grade >= L.data[key].grade){
low++;
}
Exchange(L, low, key);
key = low;
}
return key;
}
void QuickSort(List & L, int low, int high)
{
int key;
if (low < high){
key = Partition(L, low, high);
QuickSort(L, low, key - 1);
QuickSort(L, key + 1, high);
}
}
void PrintList(List L)
{
cout << "学号 " << "姓名 " << "成绩" << endl;
for (int i = 0; i < L.length; i++){
cout << L.data[i].num << " " << L.data[i].name << " " << L.data[i].grade << endl;
}
}
测试文件:
#include"QuickSort.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
List testList;
CreatList(testList, 8);
AddLast(testList, 1001, "小一", 49);
AddLast(testList, 1002, "小二", 38);
AddLast(testList, 1003, "小三", 65);
AddLast(testList, 1004, "小四", 97);
AddLast(testList, 1005, "小五", 76);
AddLast(testList, 1006, "小六", 13);
AddLast(testList, 1007, "小七", 27);
AddLast(testList, 1008, "小八", 49);
cout << "排序前:" << endl;
PrintList(testList);
cout << endl;
QuickSort(testList, 0, testList.length - 1);
cout << "排序后:" << endl;
PrintList(testList);
cout << endl;
system("pause");
}