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

#include <cstdio>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

class List;

class Item {
  friend class List;
  int n;
  Item *pastLink, *nextLink; //????????????????????????????????????
};

class List {
  Item *firstItem, *lastItem;

public:
  List();
  void insertItem(int);
  void deleteItem(int);
  void deleteFirstItem();
  void deleteLastItem();
  void showAllItem() const;
  ~List();
};

List::List() { firstItem = lastItem = NULL; }

void List::insertItem(int x) {
  Item *temp;
  temp = new Item; //??°??????????´??????????
  temp->n = x;
  if (firstItem == NULL) {
    //??????????´???????????????\\??£????????????
    temp->pastLink = temp->nextLink = NULL; //?????????????????????
    firstItem = lastItem = temp;
  } else {
    //????????????????´??????\\??£?????????
    temp->pastLink = NULL;
    temp->nextLink = firstItem;
    firstItem->pastLink = temp;
    firstItem = temp;
  }
}

void List::deleteItem(int x) {
  // x??????????????????????´??????¢???
  Item *temp;
  temp = firstItem;
  while (temp != NULL) {
    if (temp->n == x) {
      // x?????????????´??????´???
      break;
    }
    temp = temp->nextLink;
  }
  if (temp == NULL) {
    //????????????????????£?????´???
  } else {
    //???????????£?????´???
    if (temp == firstItem) {
      //???????????????????´??????´???
      deleteFirstItem();
    } else if (temp == lastItem) {
      //?????????????´??????´???
      deleteLastItem();
    } else {
      //????????\\????????´???
      temp->pastLink->nextLink = temp->nextLink;
      temp->nextLink->pastLink = temp->pastLink;
      delete temp;
    }
  }
}

void List::deleteFirstItem() {
  //???????????????????´??????????
  if (firstItem != lastItem) {
    Item *temp = firstItem;
    firstItem->nextLink->pastLink = NULL;
    firstItem = firstItem->nextLink;
    delete temp;
  } else {
    //????´?????????????????????´???
    delete firstItem;
    firstItem = lastItem = NULL;
  }
}

void List::deleteLastItem() {
  //?????????????´??????????
  if (firstItem != lastItem) {
    Item *temp = lastItem;
    lastItem->pastLink->nextLink = NULL;
    lastItem = lastItem->pastLink;
    delete temp;
  } else {
    //????´?????????????????????´???
    delete lastItem;
    firstItem = lastItem = NULL;
  }
}

List::~List() {
  //??¨??????????´??????????
  Item *temp, *temp2;
  temp = temp2 = firstItem;
  while (temp != NULL) {
    temp2 = temp;
    temp = temp->nextLink;
    delete temp2;
  }
}

void List::showAllItem() const {
  //??¨??????????´??????¨???
  Item *temp = firstItem;
  while (temp != NULL) {
    printf("%d", temp->n);
    if (temp != lastItem) {
      printf(" ");
    }
    temp = temp->nextLink;
  }
  printf("\\n");
}

int main() {
  List l;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[12];
    int x;          //???????????????
    scanf("%s", s); //??????????¨???????????????????
    if (s[0] == 'i') {
      // insert?????????????????????????????´???
      scanf("%d", &x);
      l.insertItem(x);
    } else if (strlen(s) == 6) {
      // delete?????????????????????????????´???
      scanf("%d", &x);
      l.deleteItem(x);
    } else if (s[6] == 'F') {
      // deleteFirst?????????????????????????????´???
      l.deleteFirstItem();
    } else if (s[6] == 'L') {
      // deleteLast?????????????????????????????´???
      l.deleteLastItem();
    }
  }
  l.showAllItem();
  return 0;
}

Error: TLE

Fixed submission:

/*
#include <iostream>
#include <queue>
#include <string>

using namespace std;

class List;

class Item{
    friend class List;
    int n;
    Item *pastLink,*nextLink; //????????????????????????????????????
};

class List{
    Item *firstItem,*lastItem;
public:
    List();
    void insertItem(int);
    void deleteItem(int);
    void deleteFirstItem();
    void deleteLastItem();
    void showAllItem() const;
    ~List();
};

List::List(){
    firstItem=lastItem=NULL;
}

void List::insertItem(int x){
    Item *temp;
    temp=new Item;  //??°??????????´??????????
    temp->n=x;
    if (firstItem==NULL){
        //??????????´???????????????\\??£????????????
        temp->pastLink=temp->nextLink=NULL;   //?????????????????????
        firstItem=lastItem=temp;
    }
    else{
        //????????????????´??????\\??£?????????
        temp->pastLink=NULL;
        temp->nextLink=firstItem;
        firstItem->pastLink=temp;
        firstItem=temp;
    }
}

void List::deleteItem(int x){
    //x??????????????????????´??????¢???
    Item *temp;
    temp=firstItem;
    while(temp!=NULL){
        if (temp->n==x){
            //x?????????????´??????´???
            break;
        }
        temp=temp->nextLink;
    }
    if (temp==NULL){
        //????????????????????£?????´???
    }
    else{
        //???????????£?????´???
        if (temp==firstItem){
            //???????????????????´??????´???
            deleteFirstItem();
        }
        else if (temp==lastItem){
            //?????????????´??????´???
            deleteLastItem();
        }
        else{
            //????????\\????????´???
            temp->pastLink->nextLink=temp->nextLink;
            temp->nextLink->pastLink=temp->pastLink;
            delete temp;
        }
    }
}

void List::deleteFirstItem(){
    //???????????????????´??????????
    if (firstItem!=lastItem){
        Item *temp=firstItem;
        firstItem->nextLink->pastLink=NULL;
        firstItem=firstItem->nextLink;
        delete temp;
    }
    else{
        //????´?????????????????????´???
        delete firstItem;
        firstItem=lastItem=NULL;
    }
}

void List::deleteLastItem(){
    //?????????????´??????????
    if (firstItem!=lastItem){
        Item *temp=lastItem;
        lastItem->pastLink->nextLink=NULL;
        lastItem=lastItem->pastLink;
        delete temp;
    }
    else{
        //????´?????????????????????´???
        delete lastItem;
        firstItem=lastItem=NULL;
    }
}

List::~List(){
    //??¨??????????´??????????
    Item *temp,*temp2;
    temp=temp2=firstItem;
    while(temp!=NULL){
        temp2=temp;
        temp=temp->nextLink;
        delete temp2;
    }
}

void List::showAllItem() const{
    //??¨??????????´??????¨???
    Item *temp=firstItem;
    while(temp!=NULL){
        cout<<temp->n;
        if (temp!=lastItem){
            cout<<" ";
        }
        temp=temp->nextLink;
    }
    cout<<endl;
}

int main(){
    priority_queue<int,vector<int>,greater<int> > pq;
    priority_queue<int,vector<int>,greater<int> >::iterator i;
    List l;
    int n;
    cin>>n;
    for (int i=0;i<n;i++){
        string s;
        int x;  //???????????????
        cin>>s; //??????????¨???????????????????
        if (s=="insert"){
            //insert?????????????????????????????´???
            cin>>x;
            l.insertItem(x);
            pq.push(x);
        }
        else if (s=="delete"){
            //delete?????????????????????????????´???
            cin>>x;
            i=lower_bound(pq.begin(),pq.end(),x);
            if (*i==x){
                //x???????????´???
                pq.remove(x);
                l.deleteItem(x);
            }
        }
        else if (s=="deleteFirst"){
            //deleteFirst?????????????????????????????´???
            l.deleteFirstItem();
        }
        else if (s=="deleteLast"){
            //deleteLast?????????????????????????????´???
            l.deleteLastItem();
        }
        else{
            cout<<"Error!"<<endl;
        }
    }
    l.showAllItem();
    return 0;
}
*/

#include <algorithm>
#include <iostream>
#include <list>
#include <string>

using namespace std;

int main() {
  list<int> l;
  int n;
  string cmd;
  int x;
  list<int>::iterator i, len, f;
  cin >> n;
  for (int j = 0; j < n; j++) {
    cin >> cmd;
    if (cmd[0] == 'i') {
      cin >> x;
      l.push_front(x);
    } else if (cmd == "delete") {
      cin >> x;
      f = find(l.begin(), l.end(), x);
      if (f != l.end()) {
        l.erase(f);
      }
    } else if (cmd[6] == 'F') {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }
  i = l.begin();
  for (int j = 0, s = l.size(); j < s; j++, i++) {
    cout << *i;
    if (j != s - 1) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}