Cod sursa(job #1766661)

Utilizator MoonfireFilip Sebastian Moonfire Data 28 septembrie 2016 12:20:41
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.4 kb
#include <iostream>

using namespace std;

struct nod {
    int val;
    nod* nxt;
};

class List {
public:
    int length;
    nod* handle;

    List () {
        length = 0;
        handle = new nod;
    }

    nod* Get (int _iterator) {
        if (_iterator >= 0 && _iterator < length) {
            nod* iteration = handle->nxt;
            for (int i = 0; i <= _iterator - 1; i++)
                iteration = iteration->nxt;

            return iteration;
        } else return NULL;
    }

    void Add (int _val) {
        nod* newborn = new nod;
        if (length == 0)
            handle->nxt = newborn;
        else
            Get (length - 1)->nxt = newborn;
        length++;
        Get (length - 1)->val = _val;
    }

    void Add (int _val, int _position) {
        if (_position >= 0 && _position <= length - 1) {
            nod* newborn = new nod;
            if (length == 0)
                handle->nxt = newborn;
            else {
                nod* ptrbak = Get (_position - 1)->nxt;
                Get (_position - 1)->nxt = newborn;
                Get (_position)->nxt = ptrbak;
            }
            length++;
            Get (_position)->val = _val;
        }
    }

    void RemoveAt (int _position) {
        if (_position >= 0 && _position <= length - 1) {
            nod* nxtptr = Get(_position)->nxt;
            delete Get(_position);
            if (_position == 0)
                handle->nxt = nxtptr;
            else
                Get (_position - 1)->nxt = nxtptr;

            length--;
        }
    }

    void Remove (int _val) {
        RemoveAt (Find(_val));
    }

    int Find (int _val) {
        nod* iteration = handle;
        for (int i = 0; i <= length - 1; i++) {
            iteration = iteration->nxt;
            if (iteration->val == _val)
                return i;
        }

        return -1;
    }
};

int main() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    List Anacronic;
    int load, op, val;
    cin >> load;

    for (int i = 0; i <= load - 1; i++) {
        cin >> op >> val;
        if (op == 1)
            if (Anacronic.Find(i) == -1)
                Anacronic.Add(val);

        if (op == 2)
            Anacronic.Remove(val);

        if (op == 3)
            cout << (Anacronic.Find(val) >= 0 ? 1 : 0) << '\n';
    }
}