Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
struct list {
  int val;
  list *next;
  list *prev;
};
list *head;
void init() {
  head = new list;
  head->next = head;
  head->prev = head;
}
int main() {
  using namespace std;
  int len, key;
  cin >> len;
  char str[12];
  init();
  for (int i = 0; i < len; i++) {
    scanf("%s", str);
    if (str[0] == 'i') {
      list *add = new list;
      scanf("%d", &key);
      add->val = key;
      add->next = head->next;
      head->next->prev = add;
      head->next = add;
      add->prev = head;
    } else if (strlen(str) > 6 && str[6] == 'F') {
      if (head->next != head) {
        head->next->next->prev = head;
        list *minus = head->next;
        head->next = head->next->next;
        delete minus;
      }
    } else if (strlen(str) > 6 && str[6] == 'L') {
      if (head->prev != head) {
        list *minus = head->prev;
        minus->prev->next = minus->next;
        minus->next->prev = minus->prev;
        delete minus;
      }
    } else {
      scanf("%d", &key);
      list *search = head->next;
      while (search != head && search->val != key) {
        search = search->next;
      }
      search->prev->next = search->next;
      search->next->prev = search->prev;
      delete search;
    }
  }
  list *show = head->next;
  int isf = 0;
  while (1) {
    if (show == head)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", show->val);
    show = show->next;
    delete show->prev;
  }
  printf("\n");
  delete head;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
struct list {
  int val;
  list *next;
  list *prev;
};
list *head;
void init() {
  head = new list;
  head->next = head;
  head->prev = head;
}
int main() {
  using namespace std;
  int len, key;
  cin >> len;
  char str[12];
  init();
  for (int i = 0; i < len; i++) {
    scanf("%s", str);
    if (str[0] == 'i') {
      list *add = new list;
      scanf("%d", &key);
      add->val = key;
      add->next = head->next;
      head->next->prev = add;
      head->next = add;
      add->prev = head;
    } else if (strlen(str) > 6 && str[6] == 'F') {
      if (head->next != head) {
        head->next->next->prev = head;
        list *minus = head->next;
        head->next = head->next->next;
        delete minus;
      }
    } else if (strlen(str) > 6 && str[6] == 'L') {
      if (head->prev != head) {
        list *minus = head->prev;
        minus->prev->next = minus->next;
        minus->next->prev = minus->prev;
        delete minus;
      }
    } else {
      scanf("%d", &key);
      list *search = head->next;
      while (search != head && search->val != key) {
        search = search->next;
      }
      if (search == head)
        continue;
      search->prev->next = search->next;
      search->next->prev = search->prev;
      delete search;
    }
  }
  list *show = head->next;
  int isf = 0;
  while (1) {
    if (show == head)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", show->val);
    show = show->next;
    delete show->prev;
  }
  printf("\n");
  delete head;
  return 0;
}