#include #include #include using namespace std; class Link { public: string data; Link * next; Link(string d, Link * n = NULL) { data = d; next = n; } }; class List { protected: Link * first; public: List() { first = NULL; } void add_to_front(string x) { first = new Link(x, first); } void add_to_end(string x) { Link * ptr = first, * prev = NULL; while (ptr != NULL) { prev = ptr; ptr = ptr ->next; } if (prev != NULL) prev->next = new Link(x); else first = new Link(x); } void add_in_place(string x) { Link * ptr = first, * prev = NULL; while (ptr != NULL && ptr->data <= x) { prev = ptr; ptr = ptr ->next; } if (prev != NULL) prev->next = new Link(x, ptr); else first = new Link(x, ptr); } bool is_present(string x) { Link * ptr = first; while (ptr != NULL) { if (ptr->data == x) return true; ptr = ptr->next; } return false; } void print_all() { Link * ptr = first; while (ptr != NULL) { cout << ptr->data << " "; ptr = ptr->next; } cout << "\n"; } bool remove(string x) { Link * ptr = first, * prev = NULL; while (ptr != NULL) { if (ptr->data == x) { if (prev == NULL) first = ptr->next; else prev->next = ptr->next; delete ptr; return true; } prev = ptr; ptr = ptr->next; } return false; } void reverse() { Link * r = NULL; while (first != NULL) { Link * L = first; Link * n = first->next; L->next = r; first = n; r = L; } first = r; } string remove_first() { assert(first != NULL); string r = first->data; Link * n = first->next; delete first; first = n; return r; } string remove_biggest() { assert(first != NULL); Link * ptr = first->next, * big = first, * prebig = NULL, * prev = NULL; while (ptr != NULL) { if (ptr->data > big->data) { big = ptr; prebig = prev; } prev = ptr; ptr = ptr->next; } if (prebig != NULL) prebig->next = big->next; else first = big->next; string result = big->data; delete big; return result; } bool empty() { return first == NULL; } void bubble_sort() { Link * end = NULL; while (first != end) { Link * prev = first, * ptr = first->next; while (ptr != end) { if (prev->data > ptr->data) { string temp = prev->data; prev->data = ptr->data; ptr->data = temp; } prev = ptr; ptr = ptr->next; } end = prev; } } ~List() { Link * ptr = first; while (ptr != NULL) { Link * n = ptr->next; delete ptr; ptr = n; } } }; List insertion_sort(List & L) { List M; while (! L.empty()) M.add_in_place(L.remove_first()); return M; } List selection_sort(List & L) { List M; while (! L.empty()) M.add_to_front(L.remove_biggest()); return M; } int main() { List L; string s; while (true) { cin >> s; if (s == "end") break; L.add_to_front(s); } L.bubble_sort(); L.print_all(); }