#include <iostream> #include <ctime> #define LEN 20 #define MAX_VAL 100 using namespace std; // 链式结构元素 struct elem { elem() { value = 0; next = NULL; } int value; elem * next; }; // 链式结构 struct node { node() { head = tail = NULL; } elem* head; elem* tail; }; // 比较函数 int compare_node(elem* elem_1, elem* elem_2) { if(elem_1->value < elem_2->value) { return -1; } else if(elem_1->value == elem_2->value) { return 0; } else { return 1; } } /* 对链式结构进行快速排序的partition函数, 输入是待排序链表表头 返回值首元素在partition后所在的位置的指针 需要对head进行修改, tail不用修改, tail不包含在本段内, 因此与tail无缝对接 */ elem* partition(elem** head, elem* tail, int sort_type) { elem* newHead = *head; // 本段新的链表头 elem* headPtr = *head; // 保存中间元素的位置的指针 elem* curPtr = (*head)->next; // 前驱指针 elem* prePtr = *head; while(curPtr != tail) // 遍历当前段, 直到哨兵末尾位置 { if(compare_node(headPtr, curPtr