Quick actions

cmd+k|ctrl+k

Navigation

Languages

REM(ATTACH(P,f,e),e)

Snippet info

Language

Cpp

Visibility

public

Author

hyrious

Created

2019-03-06T02:07:25Z

Updated

2019-03-06T02:07:25Z

#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;
}
INFO