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

#include <iostream>
using namespace std;

class Node {
public:
  Node *prev;
  Node *next;
  int data;

  Node(int x = 0) {
    prev = 0;
    next = 0;
    data = x;
  }
};

class List {
private:
  Node head;
  Node tail;

public:
  List() {
    head.prev = &head;
    head.next = &tail;
    tail.prev = &head;
    tail.next = &tail;
  }

  Node *search(int d) {
    Node *tmp = head.next;
    while (tmp != &tail) {
      if (tmp->data == d) {
        return tmp;
      }
      tmp = tmp->next;
    }
    return &tail;
  }

  void ins(int x) {
    Node *n;
    n = head.next;
    head.next = new Node(x);
    Node *tmp = head.next;
    tmp->prev = &head;
    n->prev = tmp;
    tmp->next = n;
  }

  void del(int x) {
    Node *tmp;
    tmp = search(x);
    Node *tmpp;
    Node *tmpn;
    tmpp = tmp->prev;
    tmpp->next = tmp->next;
    tmpn = tmp->next;
    tmpn->prev = tmp->prev;
  }

  void delfirst() {
    Node *tmp = head.next;
    head.next = tmp->next;
    Node *tmpn = tmp->next;
    tmpn->prev = &head;
  }

  void delLast() {
    Node *tmp = tail.prev;
    tail.prev = tmp->prev;
    Node *tmpp = tmp->prev;
    tmpp->next = &tail;
  }

  void print() {
    Node *tmp = head.next;
    while (tmp != &tail) {
      if (tmp->next == &tail) {
        cout << tmp->data;
      } else {
        cout << tmp->data << ' ';
      }
      tmp = tmp->next;
    }
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;
  List l;

  for (int i = 0; i < n; i++) {
    char c[20];
    int x;
    cin >> c;

    if (c[0] == 'i') {
      cin >> x;
      l.ins(x);
    } else {
      if (c[6] == 'F') {
        l.delfirst();
      } else if (c[6] == 'L') {
        l.delLast();
      } else {
        cin >> x;
        l.del(x);
      }
    }
  }
  l.print();

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

class Node {
public:
  Node *prev;
  Node *next;
  int data;

  Node(int x = 0) {
    prev = 0;
    next = 0;
    data = x;
  }
};

class List {
private:
  Node head;
  Node tail;

public:
  List() {
    head.prev = &head;
    head.next = &tail;
    tail.prev = &head;
    tail.next = &tail;
  }

  Node *search(int d) {
    Node *tmp = head.next;
    while (tmp != &tail) {
      if (tmp->data == d) {
        return tmp;
      }
      tmp = tmp->next;
    }
    return &tail;
  }

  void ins(int x) {
    Node *n;
    n = head.next;
    head.next = new Node(x);
    Node *tmp = head.next;
    tmp->prev = &head;
    n->prev = tmp;
    tmp->next = n;
  }

  void del(int x) {
    Node *tmp;
    tmp = search(x);
    Node *tmpp;
    Node *tmpn;
    tmpp = tmp->prev;
    tmpp->next = tmp->next;
    tmpn = tmp->next;
    tmpn->prev = tmp->prev;
  }

  void delfirst() {
    Node *tmp = head.next;
    head.next = tmp->next;
    Node *tmpn = tmp->next;
    tmpn->prev = &head;
  }

  void delLast() {
    Node *tmp = tail.prev;
    tail.prev = tmp->prev;
    Node *tmpp = tmp->prev;
    tmpp->next = &tail;
  }

  void print() {
    Node *tmp = head.next;
    while (tmp != &tail) {
      if (tmp->next == &tail) {
        cout << tmp->data;
      } else {
        cout << tmp->data << ' ';
      }
      tmp = tmp->next;
    }
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;
  List l;

  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s;

    if (s == "insert") {
      cin >> x;
      l.ins(x);
    } else if (s == "delete") {
      cin >> x;
      l.del(x);
    } else if (s == "deleteFirst") {
      l.delfirst();
    } else if (s == "deleteLast") {
      l.delLast();
    }
  }
  l.print();

  return 0;
}