/*
 * FileName:     doubly_linked_list_fix_2
 * CreatedDate:  2020-06-02 12:32:18 +0900
 * LastModified: 2020-06-05 15:57:56 +0900
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node{
    long int x;
    struct _node *next;
    struct _node *prev;
}str;

void print(str *list,str *bottom,str *top){
    str *l=list;
    while(l){
        printf("%ld ",l->x);
        l = l->next;
    }
    printf("\n");
    printf("top:%ld\tbottom:%ld\n",top->x,bottom->x);
}
str *create(long int x){
    str *new = malloc(sizeof(str));
    new->next=NULL;
    new->prev=NULL;
    new->x = x;
    return new;
}
str *insert(str *new,str *bottom,str *top){
    if(bottom->next){
        new->next = bottom->next;
        new->next->prev = new;
        bottom->next=new;
        new->prev = bottom;
        return top;
    }
    else{
        bottom->next=new;
        new->prev=bottom;
        return new;
    }
}

str* delete(long int x, str* bottom,str* top){
    str *list=bottom->next;
    while(list){
        if(list->x==x){
            list->prev->next=list->next;
            if(list->next){
                list->next->prev=list->prev;
            }
            else{
                top=list->prev;
            }
            list->next=NULL;
            list->prev=NULL;
            free(list);
            list=NULL;
            return top;
        }
        list=list->next;
    }
    return top;
}
void deletefirst(str *bottom){
    str *del = bottom->next;
    if(!del->next){
        del->prev->next=NULL;
        del->prev=NULL;
        free(del);
        del=NULL;
    }
    else{
        del->prev->next = del->next;
        del->next->prev = del->prev;
        del->prev=NULL;
        del->next=NULL;
        free(del);
        del=NULL;
    }
}
str *deletelast(str *top){
    str *ntop = top;
    top = top->prev;
    top->next=NULL;
    ntop->prev=NULL;
    free(ntop);
    ntop=NULL;
    return top;
}
void printlist(str *bottom,str *top){
    bottom=bottom->next;
    printf("%ld",bottom->x);
    bottom=bottom->next;
    while(bottom){
        printf(" %ld",bottom->x);
        bottom=bottom->next;
    }
    printf("\n");
}
void clearall(str *list){
    while(list){
        str *node = list;
        node->next=NULL;
        node->prev=NULL;
        free(node);
        node=NULL;
        list=list->next;
    }
}
int main(void){
    int n;scanf("%d",&n);
    str *list = malloc(sizeof(str));
    list->x=-1;
    list->next=NULL;
    list->prev=NULL;
    str *top = list,*bottom=list;
    for(int i=0;i<n;i++){
        char command[13];scanf("%s",command);
        if(strlen(command)==6){
            long int x;scanf("%ld",&x);
            if(command[0]=='i'){
                str *p = create(x);
                top = insert(p,bottom,top);
//                print(list,bottom,top);
            }
            else{
                top = delete(x,bottom,top);
//                print(list,bottom,top);
            }
        }
        else if(strlen(command)==11){
            deletefirst(bottom);
//            print(list,bottom,top);
        }
        else{
            top = deletelast(top);
//            print(list,bottom,top);
        }
    }
    printlist(bottom,top);
    clearall(list);
    return 0;
}



Measured memory_bytes: 32940