#include<stdio.h>
#include<cstdlib>
#include<cstring>

struct node{
    unsigned int key;
    struct node *next;
    struct node *prev;
};

typedef struct node * NodePointer;

NodePointer nil;

NodePointer listSearch(int key){
    /* your code */
    NodePointer np;
    for (np = nil->next; np != nil; np = np->next) {
        if(np->key == key) return np;
    }
    return NULL;
}

void init(){
    nil = (NodePointer)malloc(sizeof(struct node));
    nil->next = nil;
    nil->prev = nil;
}

void printList(){
    NodePointer cur = nil->next;
    int isf = 1;
    while(1){
        if ( cur == nil ) break;
        if ( isf == 0)  printf(" ");
        printf("%d", cur->key);
        cur = cur->next;
        isf = 0;
    }
    printf("\n");
}

void deleteNode(NodePointer t){
    /* your code */
    t->prev->next = t->next;
    t->next->prev = t->prev;
    free(t);
}

void deleteFirst(){
    NodePointer t = nil->next;
    if ( t == nil ) return;
    deleteNode(t);
}

void deleteLast(){
    /* your code */
    /*NodePointer np;
    // np->nextがnilになるまで飛ばす
    for (np = nil; np->next != nil; np = np->next) {
    }
    deleteNode(np);
     */
    deleteNode(nil->prev);
}

void deletee(int key){ // deleteは予約語(unqualified-id)
    /* your code */
    NodePointer np;
    // 一旦npにサーチ結果を代入
    np = listSearch(key);
    if(np != NULL){
        deleteNode(np);
    }
}

// 先頭(nilの次)にinsertする関数
void insert(int key){
    NodePointer x;
    x = (NodePointer)malloc(sizeof(struct node));
    x->key = key;
    
    /* your code */
    x->next = nil->next;
    x->prev = nil;
    nil->next->prev = x;
    nil->next = x;
}

int size = 0,np = 0,nd = 0;
int main(){
    int key, n, i;
    char com[20];
    scanf("%d", &n);
    init();
    
#ifdef D
    for ( i = 0; i < n; i++ ){
        scanf("%s", com);
        // insert関数
        if ( com[0] == 'i' ) {
            scanf("%d",&key);
            insert(key);
            np++;
            size++;
        }
        // serch関数
        else if(com[0] == 's') {
            scanf("%d",&key);
            if(listSearch(key) != NULL){
                printf("key %d exists\n",key);
            }else{
                printf("key %d does not exist\n",key);
            }
        }
        // delete関数系
        else if ( com[0] == 'd' ) {
            if (strlen(com) > 6){
                if ( com[6] == 'F' ) deleteFirst();
                else if ( com[6] == 'L' ) deleteLast();
            } else {
                // command:delete
                scanf("%d",&key);
                deletee(key); nd++;
            }
            size--;
        }
        printList();
    }
#else
    
    for ( i = 0; i < n; i++ ){
        scanf("%s", com);
        if ( com[0] == 'i' ) {
            scanf("%d",&key);
            insert(key);
            np++;
            size++;
        }
        else if ( com[0] == 'd' ) {
            if (strlen(com) > 6){
                if ( com[6] == 'F' ) deleteFirst();
                else if ( com[6] == 'L' ) deleteLast();
            } else {
                // command:delete
                scanf("%d",&key);
                deletee(key); nd++;
            }
            size--;
        }
    }
    
#endif
    
    printList();
    return 0;
}



Measured memory_bytes: 33812