#include #include using namespace std; struct item { string descr; int price; item(string d, int p) { descr = d; price = p; } void print() { cout << descr << " @ " << price << " cents\n"; } }; class List { protected: struct Link { item * data; Link * next; Link(item * d, Link * n = NULL) { data = d; next = n; } }; Link * first; Link * last; public: List() { first = NULL; last = NULL; } ~List() { while (first != NULL) { Link * next = first->next; delete first; first = next; } } item * find(string des) { Link * list = first; while (list != NULL) { if (list->data->descr == des) return list->data; list = list->next; } return NULL; } void add_to_end(item * it) { if (first == NULL) { first = new Link(it, first); last = first; return; } last->next = new Link(it, NULL); last = last->next; } void add_to_front(item * it) { if (first == NULL) { first = new Link(it, first); last = first; return; } first = new Link(it, first); } void print() { Link * ptr = first; while (ptr != NULL) { ptr->data->print(); ptr = ptr->next; } } bool is_present(string name) { Link * ptr = first; while (ptr != NULL) { if (ptr->data->descr == name) return true; ptr = ptr->next; } return false; } int totalprice() { Link * list = first; int total = 0; while (list != NULL) { total += list->data->price; list = list->next; } return total; } bool remove(string name) { Link * ptr = first, * prev = NULL; while (ptr != NULL) { if (ptr->data->descr == name) { if (prev == NULL) first = ptr->next; else prev->next = ptr->next; delete ptr; return true; } prev = ptr; ptr = ptr->next; } return false; } bool is_empty() { return first == NULL; } item * remove_first() { if (first == NULL) return NULL; item * value = first->data; Link * next = first->next; delete first; first = next; return value; } void delete_all() { while (first != NULL) { Link * next = first->next; delete first->data; delete first; first = next; } } }; List reverse(List & L) { List result; while (! L.is_empty()) { item * i = L.remove_first(); result.add_to_front(i); } return result; } string strip_spaces(string s) { for (int i = 0; i < s.length(); i += 1) if (s[i] != ' ') return s.substr(i); return ""; } int main() { List stock; stock.add_to_end(new item("chicken", 1000)); stock.add_to_end(new item("owl poison", 2199)); stock.add_to_end(new item("scum", 98)); stock.add_to_end(new item("nitrogen", 3267)); stock.add_to_end(new item("things", 67)); stock.add_to_end(new item("beaks", 399)); List hand; while (true) { string cmd, s; cin >> cmd; if (cmd == "stop") break; if (cmd == "stock") { stock.print(); continue; } if (cmd == "reverse") { stock = reverse(stock); continue; } getline(cin, s); s = strip_spaces(s); if (cmd == "buy") { item * it = stock.find(s); if (it == NULL) { cout << "Can't find it\n"; continue; } hand.add_to_end(it); } else if (cmd == "remove") { bool ok = stock.remove(s); if (ok) cout << "removed successfully\n"; else cout << "not found\n"; } } hand.print(); cout << "Total price is " << hand.totalprice() << "\n"; stock.delete_all(); }