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

typedef struct LINKED_ITEM {
    struct LINKED_ITEM * prev;
    int key;
    struct LINKED_ITEM * next;
} LinkedItem;

LinkedItem * createLinkedItem() {
    return (LinkedItem *)malloc(sizeof(LinkedItem));
}
void destoryLinkedItem(LinkedItem * p_item) {
    free(p_item);
}


int isEmpty(LinkedItem * p_nil) {
    return (p_nil->next == p_nil && p_nil->prev == p_nil);
}
LinkedItem * search(LinkedItem * p_nil, int targetValue) {
    LinkedItem * p_item = p_nil->next;
    
    while (p_item != p_nil && p_item->key != targetValue)
        p_item = p_item->next;
    
    // p_item == p_nil means cannot find; elegant!
    return p_item;
}
void insert(LinkedItem * p_nil, LinkedItem * p_item) {
    p_item->next = p_nil->next;
    p_nil->next->prev = p_item;
    p_nil->next = p_item;
    p_item->prev = p_nil;
}
void delete(LinkedItem * p_item) {
//    printf("deleting[p_item->key:%d] p_item->next: %p, p_item->prev: %p\n", p_item->key, p_item->next, p_item->prev);
//    printf("p_item->next->key: %7d, p_item->prev->key: %7d\n", p_item->next->key, p_item->prev->key);
//    printf("before, p_item->prev->next: %p, p_item->next->prev: %p\n", p_item->prev->next, p_item->prev);
    p_item->prev->next = p_item->next;
    p_item->next->prev = p_item->prev;
//    printf("after,  p_item->prev->next: %p, p_item->next->prev: %p\n", p_item->prev->next, p_item->prev);
    free(p_item);
}

#define MAX_OPERATION_LENGTH 12

int main(int argc, char** argv) {
    int nOperations;
    scanf("%d", &nOperations);
    
    // temp variable for scanning
    int value;
    char operation[MAX_OPERATION_LENGTH];
    
    // creating LinkedList
    LinkedItem * p_nil = createLinkedItem();
//            createLinkedItem(&p_nil);
    p_nil->prev = p_nil;
    p_nil->next = p_nil;
    
    // creating LinkedItem for use in the loop
    LinkedItem * p_item;
    
    // do the magic
    while (scanf("%s %d ", operation, &value) != EOF) {
        if (strcmp(operation, "insert") == 0) {
            p_item = createLinkedItem();
//            createLinkedItem(&p_item);
            p_item->key = value;

            insert(p_nil, p_item);
        }
        else if (strcmp(operation, "delete") == 0) {
            p_item = search(p_nil, value);

            if (p_item != p_nil)
                delete(p_item);
        }
        else if (strcmp(operation, "deleteFirst") == 0) {
            if (p_nil->next != p_nil)
                delete(p_nil->next);
        }
        
        else if (strcmp(operation, "deleteLast") == 0){
            if (p_nil->prev != p_nil)
                delete(p_nil->prev);
//            p_nil->next->next = p_nil;
//            p_nil->prev = p_nil->next;
        }
            
//        else break;
        
        
//        for (LinkedItem * p_itemPrinted = p_nil->next;  p_itemPrinted != p_nil;  p_itemPrinted = p_itemPrinted->next) 
//        {
//            printf("%d ", p_itemPrinted->key);
//        }
//        printf("; p_nil->prev.key:%d\n", p_nil->prev->key);
//        printf("p_nil, p_nil->next: %p, p_nil->prev: %p\n", p_nil->next, p_nil->prev);
//        printf("p_nil->next->key: %9d, p_nil->prev->key: %d\n", p_nil->next->key, p_nil->prev->key);

    }
    
    for (LinkedItem * p_itemPrinted = p_nil->next;  p_itemPrinted != p_nil;  p_itemPrinted = p_itemPrinted->next) 
    {
        if (p_itemPrinted != p_nil->next) printf(" ");
        printf("%d", p_itemPrinted->key);
    }
    printf("\n");

    return (EXIT_SUCCESS);
}




Measured memory_bytes: 32932