Take advantage of quick sorting, one last two pointers in the same direction
#ifndef list_h_#define list_h_#include <iostream> #include <utility>class LIST {private:struct ListNode { int _value; Listnode* _next;}; Public:list (): _head (nullptr) {}~list () {while (nullptr! = _head) {Auto TMP = _head;_head = _head->_next;delete tmp;}} insert void pushback (int value) {if (nullptr = = _head) {listnode* tmp = new ListNode (); tmp->_value = Value;tmp->_next = Nullptr;_head = tmp;} else {listnode* current = _head;while (nullptr! = current->_next) {current = Current->_next;} listnode* tmp = new ListNode (); tmp->_value = Value;tmp->_next = Nullptr;current->_next = tmp;}} sort void sort () {QuickSort (_head, nullptr);} show void display () const {Auto current = _head;while (nullptr! = current) {std::cout << current->_value << ""; current = Current->_next;} Std::cout << Std::endl;} Private:void QuickSort (listnode* First, listnode* last) {if (first! = last) {listnode* pivotptr = Partition (First, last); Q Uicksort (first, pivotptr); QuicksORT (Pivotptr->_next, last);}} listnode* Partition (listnode* First, listnode* last) {const int pivot = first->_value; listnode* i = first; listnode* j = First->_next;while (J! = last) {if (J->_value <= pivot) {i = I->_next;std::swap (I->_value, J- >_value);} j = J->_next;} Std::swap (First->_value, i->_value); return i;} Listnode* _head;}; #endif//List_h_
Single-linked list sorting--quick Sort implementation