Problem:
Doubly Linked List

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list.

deleteLast: delete the last element from the list.

Input

The input is given in the following format:

n

command1

command2

...

commandn

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst

deleteLast

Output

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

Solution in C (judge verdict: Accepted)

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

void put_uint(int n) {
    if(!n) {
        putchar_unlocked('0');
        return;
    }
    char buf[11];
    int i = 0;
    while(n) buf[i++] = (char)(n % 10 + '0'), n /= 10;
    while(i--)putchar_unlocked(buf[i]);
}
int get_uint() {
    int n = 0;
    int c = getchar_unlocked();
    if(c < 48 || 57 < c) return c;
    while(47 < c && c < 58) {
        n = 10 * n + (c & 0xf);
        c = getchar_unlocked();
    }
    return n;
}
int get_str(char *str) {
    int c;
    while((c = getchar_unlocked()) > 32) *str++ = (char)c;
    *str = 0;
    return c;
}

typedef struct ListKey {
    int num;
} ListKey;
bool listkey__equal(ListKey *a, ListKey *b) {
    return a->num == b->num;
}
typedef struct ListDat {
    ListKey key;
    struct ListDat *prev;
    struct ListDat *next;
} ListDat;
typedef struct List {
    size_t len;
    ListDat *hdr;
} List;
#define list_init(l) ((l) ? ((l)->len = 0, (l)->hdr = NULL) : 0)
#define list_len(l) ((l) ? (l)->len : 0)
#define list_delete_first(l) ((l)->len ? list__delete((l), (l)->hdr->next) : 0)
#define list_delete_last(l) ((l)->len ? list__delete((l), (l)->hdr->prev) : 0)
void list_insert(List *l, ListKey x) {
    ListDat *new_dat = malloc(sizeof(ListDat));
    if(l->len) {
        new_dat->next = l->hdr->next;
        l->hdr->next->prev = new_dat;
        l->hdr->next = new_dat;
    } else {
        l->hdr = malloc(sizeof(ListDat));
        l->hdr->prev = l->hdr->next = new_dat;
        new_dat->next = l->hdr;
    }
    new_dat->key = x;
    new_dat->prev = l->hdr;
    l->len++;
}
void list__delete(List *l, ListDat *a) {
    a->prev->next = a->next;
    a->next->prev = a->prev;
    free(a);
    l->len--;
}
void list_delete(List *l, ListKey x) {
    for(ListDat *it = l->hdr->next; it != l->hdr; it = it->next) {
        if(listkey__equal(&x, &it->key)) {
            list__delete(l, it);
            break;
        }
    }
}
void list_print(List *l) {
    for(ListDat *it = l->hdr->next; it != l->hdr->prev; it = it->next) {
        put_uint(it->key.num);
        putchar_unlocked(' ');
    }
    put_uint(l->hdr->prev->key.num);
    putchar_unlocked('\n');
}
int main(int argc, char **argv) {
    int n = get_uint();
    List l;
    list_init(&l);
    for(int i = 0; i < n; ++i) {
        char str[16];
        get_str(str);
        if(strlen(str) == 6 && strncmp(str, "insert", 6) == 0) {
            ListKey key;
            key.num = get_uint();
            list_insert(&l, key);
        } else if(strlen(str) == 6 && strncmp(str, "delete", 6) == 0) {
            ListKey key;
            key.num = get_uint();
            list_delete(&l, key);
        } else if(strlen(str) == 11 && strncmp(str, "deleteFirst", 11) == 0) {
            list_delete_first(&l);
        } else if(strlen(str) == 10 && strncmp(str, "deleteLast", 10) == 0) {
            list_delete_last(&l);
        }
    }
    list_print(&l);
    return 0;
}