标签:单链表排序
//快排,冒泡链表排序 #include<iostream> #include<assert.h> using namespace std; template<class T> struct LinkNode { T _data; LinkNode* _next; LinkNode(const T& x) :_data(x) , _next(NULL) {} }; template<class T> class ListNode { //为了安全性 private: ListNode(const ListNode& l) {} ListNode<T>& operator=(ListNode l) {} public: //程序限制 LinkNode<T>* _head; public: ListNode() :_head(NULL) {} ~ListNode() { while (_head) { PopBack(); } } void PushBack(const T& x) { LinkNode<T>* tmp = new LinkNode<T>(x); if (_head == NULL) _head = tmp; else { LinkNode<T>* cur = _head; while (cur->_next) cur = cur->_next; cur->_next = tmp; } } void PopBack() { if (_head == NULL) return; if (_head->_next == NULL) { delete _head; _head = NULL; } else { LinkNode<T>* cur = _head; while (cur->_next&&cur->_next->_next) { cur = cur->_next; } LinkNode<T>* del = cur->_next; cur->_next = NULL; delete del; } } LinkNode<T>* Search(const T& x) { if (_head == NULL) return NULL; LinkNode<T>* cur = _head; while (cur->_data != x) cur = cur->_next; return cur; } void Print() { LinkNode<T>* cur = _head; while (cur) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; } }; template<class T> LinkNode<T>* QuickPartion(LinkNode<T>* begin,LinkNode<T>* end)//前闭后开 { if (begin== end) return begin; LinkNode<T>* prev,* cur; prev = begin; cur = begin->_next; int tmp = begin->_data; while (cur != end) { if(cur->_data < tmp) { prev = prev->_next; if (cur!=prev) swap(cur->_data, prev->_data); } cur = cur->_next; } swap(prev->_data, begin->_data); return prev; } template<class T> void QuickSort(LinkNode<T>* head,LinkNode<T>* tail) { if (head == NULL || head-== tail) return; LinkNode<T>* mid = QuickPartion(head, tail); QuickSort(head, mid); QuickSort(mid->_next, tail); } template<class T> void BubbleSort(LinkNode<T>* head) { if (head == NULL || head->_next == NULL) return; LinkNode<T>* start = head; LinkNode<T>* end = NULL; LinkNode<T>* curBegin = NULL; int flag = 0; while (start->_next) { curBegin = start; flag = 0; while (curBegin->_next != end) { if (curBegin->_data > curBegin->_next->_data) { swap(curBegin->_data, curBegin->_next->_data); flag = 1; } curBegin = curBegin->_next; } if (flag == 0) break;//优化,若有序,直接跳出 end = curBegin; } } template<class T> LinkNode<T>* SearchMidNode(LinkNode<T>* head) { if (head == NULL || head->_next == NULL) return head; LinkNode<T>* slow = head; LinkNode<T>* fast = head; while (fast&&fast->_next)//偶数个数中间任意一个 //while (fast&&fast->_next&&fast->_next->_next)//偶数个数找到中间的第一个 { fast = fast->_next->_next; slow = slow->_next; } return slow; } void Test1() { ListNode<int> l1; l1.PushBack(10); l1.PushBack(9); l1.PushBack(8); l1.PushBack(7); l1.PushBack(6); l1.PushBack(6); l1.PushBack(5); l1.PushBack(9); l1.PushBack(0); l1.PushBack(1); l1.PushBack(2); /*LinkNode<int>* tail = NULL; QuickSort(l1._head,tail);*/ //BubbleSort(l1._head); cout << SearchMidNode(l1._head)->_data<< endl; l1.Print(); }
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1767293
标签:单链表排序
原文地址:http://10541556.blog.51cto.com/10531556/1767293