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

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

template <class T> class Node {
private:
  T data;
  Node<T> *prev;
  Node<T> *next;

public:
  Node() {
    data = -1;
    prev = NULL;
    next = NULL;
  }
  ~Node() {
    if (prev) {
      prev->setNext(next);
    }
    if (next) {
      next->setPrev(prev);
    }
  }
  T getData() { return data; }
  void setData(T val) { data = val; }
  Node<T> *getPrev() { return prev; }
  Node<T> *getNext() { return next; }
  void setPrev(Node<T> *node) { prev = node; }
  void setNext(Node<T> *node) { next = node; }
  void insertBefore(Node<T> *node) {
    prev = node->getPrev();
    next = node;

    if (node->getPrev()) {
      node->getPrev()->setNext(this);
    }
    node->setPrev(this);
  }
  void insertAfter(Node<T> *node) {
    prev = node;
    next = node->getNext();

    if (node->getNext()) {
      node->getNext()->setPrev(this);
    }
    node->setNext(this);
  }
};

int main() {
  int n, x;
  bool f;
  string s;
  Node<int> *root, *first, *tmp;

  root = new Node<int>;
  root->setNext(root);
  root->setPrev(root);

  cin >> n;

  while (n--) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      tmp = new Node<int>;
      tmp->setData(x);
      tmp->insertAfter(root);
    } else if (s == "delete") {
      cin >> x;
      tmp = root;
      f = false;
      while (tmp->getNext() != root) {
        tmp = tmp->getNext();
        if (tmp->getData() == x) {
          f = true;
          break;
        }
      }
      if (f) {
        delete tmp;
      }
    } else if (s == "deleteFirst") {
      delete root->getNext();
    } else if (s == "deleteLast") {
      delete root->getPrev();
    }
  }

  tmp = root;
  while (1) {
    if (tmp->getNext() == root) {
      break;
    }
    tmp = tmp->getNext();
    if (tmp->getNext() == root) {
      cout << tmp->getData();
    } else {
      cout << tmp->getData() << ' ';
    }
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

template <class T> class Node {
private:
  T data;
  Node<T> *prev;
  Node<T> *next;

public:
  Node() {
    data = -1;
    prev = NULL;
    next = NULL;
  }
  ~Node() {
    if (prev) {
      prev->setNext(next);
    }
    if (next) {
      next->setPrev(prev);
    }
  }
  T getData() { return data; }
  void setData(T val) { data = val; }
  Node<T> *getPrev() { return prev; }
  Node<T> *getNext() { return next; }
  void setPrev(Node<T> *node) { prev = node; }
  void setNext(Node<T> *node) { next = node; }
  void insertBefore(Node<T> *node) {
    prev = node->getPrev();
    next = node;

    if (node->getPrev()) {
      node->getPrev()->setNext(this);
    }
    node->setPrev(this);
  }
  void insertAfter(Node<T> *node) {
    prev = node;
    next = node->getNext();

    if (node->getNext()) {
      node->getNext()->setPrev(this);
    }
    node->setNext(this);
  }
};

template <class T> void printNodes(Node<T> *node) {
  while (1) {
    if (node->getNext() == NULL) {
      break;
    }
    node = node->getNext();
    if (node->getNext()) {
      cout << node->getData() << ' ';
    } else {
      cout << node->getData();
    }
  }
  cout << endl;
}

int main() {
  int n, x;
  bool f;
  string s;
  Node<int> *first, *tmp;

  first = new Node<int>;
  cin >> n;

  while (n--) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      tmp = new Node<int>;
      tmp->setData(x);
      tmp->insertAfter(first);
    } else if (s == "delete") {
      cin >> x;
      tmp = first;
      f = false;
      while (tmp->getNext()) {
        tmp = tmp->getNext();
        if (tmp->getData() == x) {
          f = true;
          break;
        }
      }
      if (f) {
        delete tmp;
      }
    } else if (s == "deleteFirst") {
      delete first->getNext();
    } else if (s == "deleteLast") {
      tmp = first;
      while (tmp->getNext()) {
        tmp = tmp->getNext();
      }
      delete tmp;
    }
  }

  printNodes(first);

  return 0;
}