Templates - Data structure - list / LinkedList

Delete only for a doubly linked list, in order not to introduce a simple head node, and does not carry out the next set of her operation. Space using slightly more often, but harmless.

struct LinkedList {
    static const int MAXN = 100000;
    int n, prev[MAXN + 5], next[MAXN + 5];

    void Init(int _n) {
        n = _n;
        for(int i = 1; i <= n; ++i) {
            prev[i] = i - 1;
            next[i] = i + 1;
        }
        prev[1] = -1;
        next[n] = -1;
    }

    void Remove(int x) {
        prev[next[x]] = prev[x];
        next[prev[x]] = next[x];
    }
};

The normal list is singly linked list, delete to delete the current node is the next node, there is no need. Everything is written in both directions. b can also write something more garbage collection.

struct LinkedList {
    static const int MAXN = 100000;
    int n, prev[MAXN + 5], next[MAXN + 5];
    static const int head = 0, tail = MAXN + 1;

    void Init() {
        n = 0;
        prev[head] = -1;
        next[head] = tail;
        prev[tail] = head;
        next[tail] = -1;
    }

    int Insert(int x) {
        ++n;
        prev[next[x]] = n;
        next[n] = next[x];
        prev[n] = x;
        next[x] = n;
        return n;
    }

    void Remove(int x) {
        prev[next[x]] = prev[x];
        next[prev[x]] = next[x];
    }
};

Disjoint-set pseudo-list implementation.

struct PrevLinkedList {
    int n;
    bool vis[100005];
    int prev[100005];

    void Init(int _n) {
        n = _n;
        for(int i = 1; i <= n; ++i) {
            vis[i] = 0;
            prev[i] = i - 1;
        }
    }

    int Prev(int x) {
        int r = prev[x];
        while(vis[r])
            r = prev[r];
        int t;
        while(prev[x] != r) {
            t = prev[x];
            prev[x] = r;
            x = t;
        }
        return r;
    }

    void Remove(int x) {
        vis[x] = 1;
    }
};

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/11925448.html