Single-link list

xiaoxiao2021-03-06  38

This is a topic of the hottey interview before: Do not use the external array, only the list of the list itself is used to complete the sort. I feel very interesting, I have realized one. The program code is as follows:

#include using namespace std;

Template struct node // node structure {node * next; t data;};

Template Class Slist // Single-Load Table Structure {Public: Slist () {head.next = null;} ~ slist () {clear ();} void clear () // Clear Link list {Node * p; while (head.next! = null) {p = head.next; head.next = p-> next; delete p;}} void push_back (t t) / / For simplified programs, only Push_Back interface completion elements Insert {node * p = & head; while (p-> next! = Null) P = p-> next; Node * q = new node ; Q-> Data = T; Q- > next = null; p-> next = q;} void print () / / For simplified programs, no overloading of Operator <<, direct use of member function output {node * p; for (p = Head.next; p! = null; p = p-> next) cout << p-> data;} void} void} void} void} void} void} void} void} void} / {ix (begin) // fast row {ix == END) // Recursive Exit Condition Return; T T; Node * P, * q; T = Begin-> Data; Q = Begin; // q as the Sprinter Pointer for (P = Begin-> Next; P! = end; p = p-> next) // One fast row {IF (t> p-> data) {q-> data = p-> data; p-> Data = T; Q = P;} } QKSORT (Begin, Q); // Sentinel QKSORT (Q-> next, end); // The quick row after the sentinel } Void sort () // call fast row completion sort {qksort (head.next, null);} private: Node head;};

INT main () {slist L; for (int i = 5; I> = 0; I -) L.Push_back (i); l.print (); cout << endl; l.sort () L.Print (); return 0;} Postscript: This can also be seen that the STL is a single column of a Sort algorithm in the STL.

转载请注明原文地址:https://www.9cbs.com/read-68951.html

New Post(0)