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 <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
    int key;
    struct node *prev;
    struct node *next;
};
typedef struct node *NodePointer;

NodePointer origin;

void setNodeKey(NodePointer, int);
void setNodePrev(NodePointer, NodePointer);
void setNodeNext(NodePointer, NodePointer);

NodePointer search(int);
void insert(int);
void delete(NodePointer);

void display();

int main() {
    int i;
    int n, input;
    char command[15];

    scanf("%d", &n);

    origin = (NodePointer)malloc(sizeof(struct node));
    setNodePrev(origin, origin);
    setNodeNext(origin, origin);

    for (i = 0; i < n; i++) {
        scanf("%s", command);
        if (strcmp("insert", command) == 0) {
            scanf("%d", &input);
            insert(input);
        }
        else if (strcmp("delete", command) == 0) {
            scanf("%d", &input);
            delete(search(input));
        }
        else if (strcmp("First", &command[6])) {
            delete(origin->prev);
        }
        else if (strcmp("Last", &command[6])) {
            delete(origin->next);
        }
    }
    display();

    return 0;
}

void setNodeKey(NodePointer n, int key) {
    n->key = key;
}

void setNodePrev(NodePointer n, NodePointer prev) {
    n->prev = prev;
}

void setNodeNext(NodePointer n, NodePointer next) {
    n->next = next;
}

NodePointer search(int key) {
    NodePointer target = origin->next;

    // 探しているノード( target ) == 最初のノード (origin) になったら一周
    while (target != origin) {
        // 探しているノード( target )と'key'が一致した場合
        if (target->key == key) {
            return target;
        }
        target = target->next;
    }

    return NULL;
}

void insert(int key) {
    NodePointer n;
    // 現在 : ( origin ) <-> ( 0 )...
    // 完成 : ( origin ) <-> ( n ) <-> ( 0 )...

    // 「新規追加ノード( n )」のprevを「最初のノード( origin )」にする
    // 「新規追加ノード( n )」のnextを「最初のノードの次のノード( 0 )」にする
    n = (NodePointer)malloc(sizeof(struct node));
    setNodePrev(n, origin);
    setNodeNext(n, origin->next);
    setNodeKey(n, key);

    // 「最初のノードの次のノード( 0 )」のprevを「新規追加ノード( n )」にする
    setNodePrev(origin->next, n);
    // 「最初のノード( origin )」のnextを「新規追加ノード( n )」にする
    setNodeNext(origin, n);
}

void delete(NodePointer n) {
    if (n == NULL) { return; }
    // 現在 : ...( 0 ) <-> ( n ) <-> ( 2 )...
    // 完成 : ...( 0 ) <-> ( 2 )...

    // 「取消対象ノードの次のノード( 2 )」のprevを「取消対象ノードの前のノード( 0 )」にする
    setNodePrev(n->next, n->prev);
    // 「取消対象ノードの前のノード( 0 )」のnextを「取消対象ノードの次のノード( 2 )」にする
    setNodeNext(n->prev, n->next);
    // 「取消対象ノード」を開放する
    free(n);
}

void display() {
    NodePointer target = origin->next;

    // 探しているノード( target ) == 最初のノード (origin) になったら一周
    while (target != origin) {
        if (target != origin->next) printf(" ");
        printf("%d", target->key);

        target = target->next;
    }
    printf("\n");
}