/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   BestCode.c
 * Author: kundaikwangwari
 *
 * Created on July 24, 2019, 9:12 AM
 */
#include <stdio.h>
#include <stdlib.h>
#include  <math.h>
#include <ctype.h>
#include <string.h>
#define First -200
#define Last -300

/*
 * 
 */

typedef struct node {
    int Value;
    struct node *next;
    struct node *prev;
}data;

struct node * root, *head, *newNode, *tail;

void display() {  
    
    struct node* temp;
    temp = tail;
    //printf("%p\n",temp);
    while(temp != NULL){
        
        if(temp == head){
            printf("%d",head->Value);
            break;
        }
        printf("%d",temp->Value);
        temp = temp->prev;
       printf(" ");
        //printf("%p\n",temp);
    }
    printf("\n");
    return;
}

void insert(int Value){
    
    //printf("inserting %d\n",Value);
    newNode = (struct node*)malloc(sizeof(struct node));
    if(newNode == NULL){
        printf("memory allocation failed. Run terminated...\n");
        return;
    }
    
    newNode->Value = Value;
    newNode->prev = NULL;
    newNode->next = NULL;
        
     if(root == NULL){
 
        head = newNode;
        tail = head;
        root = head;
        //display();

        return;
     }
    
         
    newNode->prev = tail;
    tail->next = newNode;
    tail = newNode;
    //display();
    return;
    }
    
void deleteNum(int num){
    
    //printf("deleting %d\n",num);
    struct node *temp, *toDelete;
    temp = tail;
    
    if(temp == NULL){
        //printf("List is empty\n");
        return;
    }
    
    if((num == First) || (num == tail->Value)) {
        toDelete = tail;
        tail = tail->prev;
        tail->next = NULL;
        free(toDelete);
        //display();
        return;
    }
    
     if(num == Last){
        toDelete = head;
        if(head->next != NULL){
        head = head->next;
        head->prev = NULL;
        free(toDelete);
        //display();
        return;
        }
        else free(toDelete);
        return;
    }
    
    while(temp != NULL){
        //printf("found %d\n",temp->Value);
        if(temp->Value == num){
            if(temp == head){
        toDelete = head;
        head = head->next;
        head->prev = NULL;
        free(toDelete);
        //display();
        return;
    }
         toDelete = temp;
         temp->prev->next = toDelete->next;
         temp->next->prev = toDelete->prev;
         free(toDelete);
         //display();
         return;
        }
        else temp = temp->prev;
    }
    //printf("Value %d is not present\n",num);
}

 

int main(int argc, char** argv) {
    
    char instruction1[7] = "insert";
    char instruction2[7] = "delete";
    char instruction3[12] ="deleteFirst";
    char instruction4[12]= "deleteLast";
    
    int n;
    scanf("%d\n",&n);
    char operation[50];
    int lim = 0, numlim=0, num;
    int complete = 1;
    char number[15];
    

   
    for(int i = 0; i < n; ){
       
        char C = getchar();
        
        if(isalpha(C)){
            operation[lim++] = C;
            continue;
        }

        
        if((C == '\n') || (isspace(C))){
            operation[lim++] = '\0';
            lim = 0;
            if(strcmp(operation,instruction3) == 0){
             deleteNum(First);
             i++;
             continue;   
            }
            
            if(strcmp(operation,instruction4) == 0){
               deleteNum(Last);
                i++;
               continue; 
            }
                complete = 0;
                while(complete == 0){
                    C = getchar();
                    
                    
                    if(isdigit(C)){
                    number[numlim++] = C;
                    continue;
                    }
                    
                    if((C == '\n') || (isspace(C))){
                    number[numlim++] = '\0';
                    num = atoi(number);
                    numlim=0;
                            if(strcmp(operation,instruction2) == 0){
                                   deleteNum(num);
                                    complete = 1;
                                    i++;
                            }
                            if(strcmp(operation,instruction1) == 0){
                                    insert(num);
                                    i++;
                                    complete=1;
                            }
                    }
            }

      }
    } 
 display();
    return (0);
     
}



