#include <iostream>
using namespace std;
struct P { int factor, exp; P *next; };
P ZERO;
P *attach(P *p, int factor, int exp) {
P *q = p;
while (q->next) q = q->next;
q->next = new P { factor, exp, nullptr };
return p;
}
void print(P *p) {
if (p == &ZERO) return print(p->next);
cout << p->factor << "x^" << p->exp;
if (p->next) { cout << "+"; print(p->next); }
}
P *rem(P *p, int exp) {
if (p->next) p->next = rem(p->next, exp);
if (p != &ZERO && p->exp == exp) {
P *q = p->next;
delete p;
return q;
} else {
return p;
}
}
int main() {
P *p = rem(attach(attach(attach(attach(&ZERO, 10, 1), -12, 3), -10, 1), 0, 2), 1);
print(p);
return 0;
}