/*
* @Author: hzf
* @Date: 2020-03-01 22:24:23
* @Last Modified by: hzf
* @Last Modified time: 2020-03-01 22:59:53
*/
/*
一般方法:
在数组中进行快排,然后放入单链表中
*/
/*优化算法
通过六个变量实现单链表中小于num的数字在左边,等于在中间,大于的在右边
实现方法:
建立六个指针分别指向小于区的首尾
等于区的首尾
大于区的首尾
然后对原链表进行遍历,分别放在相应的首尾指针中
最后分类讨论是否有空指针,并且把首尾指针串起来
*/
#include <iostream>
using namespace std;
class Node
{
public:
Node(int data)
{
this.value = dat ;
}
int value;
Node* next;
};
Node* listPartitionq(Linklist head, int pivot)
{
Node sH = NULL;//small head
Node sT = NULL;//small tail
Node eH = NULL;//equal head
Node eT = NULL;//equal tail
Node bH = NULL;//big head
Node bT = NULL;//big tail
Node next = NULL;//save next node
while(head != NULL){
next = head.next;
head.next = NULL;
if (head.value < pivot)
{
if (sH == NULL)
{
sH = head;
sT = head;
}else{
sH.next = head
sT = head;
}
else if (head.value == pivot)
{
if (eH == NULL)
{
eH = head;
eT = head;
}else{
eH.next = head;
eT = head;
}
}
else{
if (bH == NULL)
{
bH = head;
bT = head
}else{
bH.next = head;
bT = head;
}
}
}
}
head = next;
//small and equal reconnect
if (sT != null) {
sT.next = eH;
eT = eT == null ? sT : eT;
}
// all reconnect
if (eT != null) {
eT.next = bH;
}
return sH != null ? sH : eH != null ? eH : bH;
}
int main(int argc, char const *argv[])
{
/* code */
return 0;
}
将单链表中的元素重新排列,小的在左边,相等的在中间,大的在右边(优化算法-C++)
猜你喜欢
转载自blog.csdn.net/qq_41582910/article/details/104602632
今日推荐
周排行