#include "stdafx.h" #include #include #include using namespace std; const int K = 5; const char minc = ' '; const char maxc = 127; struct Item { char a [K]; Item * next; Item () : next (nullptr) { } }; struct List { Item * first; Item * last; List () : first (nullptr), last (nullptr) { } void addLast (Item * p); Item * unlinkFirst (); void join(List& L); }; void List::join(List& L){ if (L.first!=nullptr) { if (first==nullptr) first=L.first; else last->next=L.first; last=L.last; L.first=nullptr; L.last=nullptr; } } Item* List::unlinkFirst() { Item* p = first; if ( p != nullptr) { first = p->next; p->next = nullptr; } return p; } void List::addLast (Item * p) { p->next = nullptr; if (first == nullptr) first = p; else last->next = p; last = p; } List queue; void add (const char * s) { Item * p = new Item; int len = strlen (s); int i = 0; while (i < K && i < len) { char c = s[i]; if (c < minc) c = minc; if (c > maxc) c = maxc; p->a[i] = c; i ++; } while (i < K) { p->a[i] = minc; i ++; } queue.addLast (p); } void split (int p) { List tab [maxc-minc+1]; while (queue.first != nullptr) { Item * item = queue.unlinkFirst(); char x = item->a[p]; tab[x-minc].addLast(item); } // queue.first = nullptr; // queue.last = nullptr; for (int i = minc; i <= maxc; i++) queue.join(tab[i-minc]); } int _tmain(int argc, _TCHAR* argv[]) { add ("ahoj"); add ("abc"); add ("ale"); for (int p = K-1; p >= 0; p--) split (p); Item * u = queue.first; while (u != nullptr) { for (int p = 0; p < K; p++) cout << u->a[p]; cout << endl; u = u->next; } cout << "Konec - stisknete enter" << endl; char c; cin >> c; return 0; }