#include #include using namespace std; struct Link { string data; Link * next; Link(string d, Link * n); }; struct List { Link * first, * last; List(); void insert(string s); void add_at_end(string s); void add_all_to_end(List x); void printall(); bool not_empty(); string remove_first(); void split(List & x); }; Link::Link(string d, Link * n) { data = d; next = n; } List::List() { first = NULL; last = NULL; } void List::insert(string s) { first = new Link(s, first); if (last == NULL) last = first; } void List::add_at_end(string s) { if (first == NULL) { first = new Link(s, first); last = first; } else { last->next = new Link(s, NULL); last = last->next; } } void List::add_all_to_end(List x) { if (first == NULL) { first = x.first; last = x.last; } else { last->next = x.first; if (x.last != NULL) last = x.last; } } bool List::not_empty() { return first != NULL; } string List::remove_first() { if (first == NULL) { cerr << "Removing from empty list\n"; exit(1); } string s = first->data; first = first->next; if (first == NULL) last = NULL; return s; } void List::printall() { Link * ptr = first; while (ptr != NULL) { cout << ptr->data << " "; ptr = ptr->next; } if (last == NULL) cout << "(last=NULL)\n"; else cout << "(last=" << last->data << ")\n"; } void List::split(List & x) { Link * ptr1 = first, * ptr2 = first, * prev2 = NULL; while (true) { if (ptr1 == NULL) break; ptr1 = ptr1->next; if (ptr2 == NULL) break; prev2 = ptr2; ptr2 = ptr2->next; if (ptr1 == NULL) break; ptr1 = ptr1->next; } if (prev2 == NULL) { x.first = NULL; x.last = NULL; } else { prev2->next = NULL; x.first = ptr2; x.last = last; last = prev2; } if (x.first == NULL) x.last = NULL; } List merge(List a, List b) { List r; while (a.not_empty() && b.not_empty()) { string x; if (a.first->data < b.first->data) x = a.remove_first(); else x = b.remove_first(); r.add_at_end(x); } if (a.not_empty()) r.add_all_to_end(a); else r.add_all_to_end(b); return r; } int main() { List a, b; while (true) { string s; cin >> s; if (s == "****") break; a.add_at_end(s); } while (true) { string s; cin >> s; if (s == "****") break; b.add_at_end(s); } List c = merge(a, b); cout << "a: "; a.printall(); cout << "b: "; b.printall(); cout << "c: "; c.printall(); }