#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef struct tagSList {
    int key;
    tagSList *pNext;
    tagSList *pPrev;
} SList;

class DoubleList
{
public :
    DoubleList(void) {
        m_sHead.key = 0;
        m_sHead.pNext = &m_sHead;
        m_sHead.pPrev = &m_sHead;
    };
    ~DoubleList(void);
    bool Insert(int key);
    bool Delete(int key);
    bool DeleteFirst(void);
    bool DeleteLast(void);
    void PrintAll(void);
private :
    SList m_sHead;
};

DoubleList::~DoubleList(void)
{
    SList *pList;
    SList *pTmp;
    pList = m_sHead.pNext;
    while (pList != &m_sHead) {
        pTmp = pList->pNext;
        free(pList);
        pList = pTmp;
    }
    return;
}

void DoubleList::PrintAll(void)
{
    SList *pList;
    bool first = true;

    pList = m_sHead.pNext;
    while (pList != &(m_sHead)) {
        if (first == false) {
            printf(" ");
        }
        printf("%d", pList->key);
        pList = pList->pNext;
        first = false;
    }
    printf("\n");

    return;
}

bool DoubleList::Insert(int key)
{
    bool ret;
    SList *pList;

    pList = (SList*)malloc(sizeof(SList));
    if (pList != NULL) {
        ret = true;
        pList->key    = key;
        pList->pNext  = m_sHead.pNext;
        pList->pPrev  = &m_sHead;
        m_sHead.pNext = pList;
        (pList->pNext)->pPrev = pList;
    }
    else {
        ret = false;
    }

    return ret;
}

bool DoubleList::Delete(int key)
{
    bool ret = false;
    SList *pList;

    pList = m_sHead.pNext;

    while (pList != &m_sHead) {
        if (pList->key == key) {
            ret = true;
            (pList->pPrev)->pNext = pList->pNext;
            (pList->pNext)->pPrev = pList->pPrev;
            free(pList);
            break;
        }
        else {
            pList = pList->pNext;
        }
    }

    return ret;
}

bool DoubleList::DeleteFirst(void)
{
    bool ret;
    SList *pList;

    pList = m_sHead.pNext;
    if (pList != &m_sHead) {
        ret = true;
        m_sHead.pNext = pList->pNext;
        (pList->pNext)->pPrev = &m_sHead;
        free(pList);
    }
    else {
        ret = false;
    }

    return ret;
}

bool DoubleList::DeleteLast(void)
{
    bool ret;
    SList *pList;

    pList = m_sHead.pPrev;
    if (pList != &m_sHead) {
        ret = true;
        (pList->pPrev)->pNext = &m_sHead;
        m_sHead.pPrev = pList->pPrev;
        free(pList);
    }
    else {
        ret = false;
    }

    return ret;
}

int main(void)
{
    int i;
    int num;
    char order[20];
    int  key;
    DoubleList cList;

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%s", order);
        if (strlen(order) > 6) {
            switch (order[6]) {
            case 'F' :
                cList.DeleteFirst();
                break;
            case 'L' :
                cList.DeleteLast();
                break;
            default :
                printf("Unknwon!!!\n");
                break;
            }
        }
        else {
            scanf("%d", &key);

            switch (order[0]) {
            case 'i' :
                cList.Insert(key);
                break;
            case 'd' :
                cList.Delete(key);
                break;
            default :
                printf("Unknown !!!\n");
                break;
            }
        }
    }

    cList.PrintAll();

    return 0;
}

Measured memory_bytes: 32236