您好,登錄后才能下訂單哦!
//快排,冒泡鏈表排序 #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(); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。