#include <stdio.h>
#include <stdlib.h>

struct lst2 {
    int x;
    struct lst2 *n, *p;
};

struct dp {
    struct lst2 *f, *l;
};

void prtdp(struct dp *p) {
    struct lst2 *f = p->f;
    for ( ; f; f = f->n)
        printf("%d ", f->x);
    printf("\n");
}

void prtdpr(struct dp *p) {
    struct lst2 *l = p->l;
    for ( ; l; l = l->p)
        printf("%d ", l->x);
    printf("\n");
}

struct dp *initdp() {
    struct dp *p = malloc(sizeof(*p));
    p->f = NULL;
    p->l = NULL;
    return p;
}

struct dp *appdp(const int a[], int n) {
    struct lst2 *t;
    struct dp *p = initdp();
    for (int i = 0; i < n; i++) {
        t = malloc(sizeof(*t));
        t->x = a[i];
        t->n = NULL;
        if (!p->f) {
            p->f = t;
            t->p = NULL;
        } else {
            p->l->n = t;
            t->p = p->l;
        }
        p->l = t;
    }
    return p;
}

void insrht(struct dp *p, struct lst2 *c, int x) {
    struct lst2 *t;

    t = malloc(sizeof(*t));
    t->x = x;

    t->p = c;
    if (c) {
        t->n = c->n;
        c->n = t;
    }
    else {
        t->n = p->f;
        p->f = t;
    }

    if (t->n)
        t->n->p = t;
    else
        p->l = t;
}

void inslft(struct dp *p, struct lst2 *c, int x) {
    struct lst2 *t;

    t = malloc(sizeof(*t));
    t->x = x;

    t->n = c;
    if (c) {
        t->p = c->p;
        c->p = t;
    } else {
        t->p = p->l;
        p->l = t;
    }

    if (t->p)
        t->p->n = t;
    else
        p->f = t;
}

void rmvdp(struct dp *p, struct lst2 *c, int *x) {
    struct lst2 *t;

    t = c;
    *x = t->x;

    if (c->p)
        c->p->n = c->n;
    else
        p->f = c->n;

    if (c->n)
        c->n->p = c->p;
    else
        p->l = c->p;

    free(t);
}

int main() {
    int x;
    struct dp *p;
    int a[] = {25, 36, 49};
    int n = sizeof(a)/sizeof(*a);
    p = appdp(a, n);
    insrht(p, p->f->n, 44);
    prtdp(p);
    rmvdp(p, p->l, &x);
    printf("%d\n", x);
    prtdp(p);
    return 0;
}
