Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int main() {
  list<int> il;
  char command[20];
  int n;
  int key;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%s", command);
    if (command[0] == 'i') { // insert
      scanf("%d", &key);
      il.push_front(key);
    } else if ((command[0] == 'd') && (command[6] == 'F')) {
      il.pop_front();
    } else if ((command[0] == 'd') && (command[6] == 'L')) {
      il.pop_back();
    } else if (command[0] == 'd') {
      scanf("%d", &key);
      for (list<int>::iterator it = il.begin(); it != il.end(); it++) {
        if (*it == key) {
          il.erase(it);
          break;
        }
      }
    }
  }
  int i = 0;
  for (list<int>::iterator it = il.begin(); it != il.end(); it++) {
    if (i++)
      printf(" ");
    printf("%d", *it);
  }
  printf("\\n");
  return 0;
}

Error: TLE

Fixed submission:

/*Doubly Linked List*/
#include <cstdio>
#include <cstdlib>
#include <cstring>

typedef struct _n {
  int key;
  struct _n *prev;
  struct _n *next;
} node;

node *p_nil; //????????????????????¨???

void init() { //??????nil????????????????????????
  p_nil = (node *)malloc(sizeof(node));
  p_nil->prev = p_nil;
  p_nil->next = p_nil;
  p_nil->key = -1;
} //??????????????????????????????
void deletenode(node *p) {
  free(p);
} //?????????????????????????????¢?????????????§£??? //??????????????????
void deletelist(node *p) {
  if (p->prev == p_nil) {
    deletenode(p);
  } else {
    deletelist(p->prev);
    deletenode(p);
  }
}
void insert(int key) {
  node *new_node = (node *)malloc(sizeof(node));
  node *prev_head = p_nil->prev;

  /*?????\\???????????????????????????????????????*/
  prev_head->next = new_node;

  /*nil???????????????????????????*/
  p_nil->prev = new_node;

  /*??°???????????????????????????????????????*/
  new_node->key = key;
  new_node->next = p_nil;
  new_node->prev = prev_head;
}
void print_node() {
  node *head_node = p_nil->prev;
  int isfirst = 1;
  if (head_node == p_nil) {
    printf("Doubly Linked List is empty\\n");
  } else {
    while (head_node != p_nil) {
      if (isfirst != 1) {
        printf(" ");
      }
      printf("%d", head_node->key);
      head_node = head_node->prev;
      isfirst = 0;
    }
    printf("\\n");
  }
}
void deleteKey(int key) {
  int flag = 0;
  node *p_node = p_nil->prev;
  while (p_node != p_nil) {
    if (p_node->key == key) {
      flag = 1;
      (p_node->next)->prev = p_node->prev;
      (p_node->prev)->next = p_node->next;
      free(p_node);
      break;
    } else {
      p_node = p_node->prev;
    }
  }
  /*
  if(flag == 0){
    printf("error: the selected key is not cintained in the List.\\n");
  }
  */
}
void deleteFirst() {
  if (p_nil->prev != p_nil) {
    node *deletenode = p_nil->prev;

    /*???????????´??°*/
    p_nil->prev = deletenode->prev;

    /*?????????????????????????????????????????????*/
    (deletenode->prev)->next = p_nil;

    /*??????????????????*/
    free(deletenode);
  }
}
void deleteLast() {
  node *current_node;
  if (p_nil->prev != p_nil) {
    current_node = p_nil->prev;
    while (current_node->prev != p_nil)
      current_node = current_node->prev;
    (current_node->next)->prev = p_nil;
    free(current_node);
  }
}
void print_node_detail(node *p) {
  printf("key:%d\\n", p->key);
  printf("next node key:%d\\n", (p->next)->key);
  printf("prev node key:%d\\n", (p->prev)->key);
}
void print_list_detail() {
  node *current_node = p_nil;
  int itr = 0;
  while (current_node->prev != p_nil) {
    itr += 1;
    printf("node number:%d\\n", itr);
    print_node_detail(current_node);
    current_node = current_node->prev;
  }
}

int main() {
  /*??\\???*/
  int n, key, i;
  init();
  std::scanf("%d\\n", &n);
  char com[20];
  for (i = 0; i < n; i++) {
    std::fgets(com, 20, stdin);
    if (com[0] == 'i') {   // insert4
      key = atoi(&com[7]); // int atoi(const char *str)
      insert(key);
    } else if (com[0] == 'd' && com[6] == 'F') {
      deleteFirst();
    } else if (com[0] == 'd' && com[6] == 'L') {
      deleteLast();
    } else {
      key = atoi(&com[7]);
      deleteKey(key);
    }
  }
  print_node();
  deletelist(p_nil);
  return 0;
}