Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// use printf, scanf instead of cout, cin.

using namespace std;

template <typename T> struct Node {
  Node(const T &value) : mValue(value), mNext(nullptr), mPrev(nullptr) {}

  T mValue;

  Node<T> *mNext;
  Node<T> *mPrev;
};

template <typename T> class List {
private:
  Node<T> *mFront;
  Node<T> *mBack;

public:
  List() : mFront(nullptr), mBack(nullptr) {}
  ~List() {
    while (mFront)
      popFront();
  }

  void pushFront(const T &val) {
    auto node(new Node<T>(val));

    if (mFront) {
      node->mNext = mFront;
      mFront->mPrev = node;

      mFront = node;
    } else {
      mFront = node;
      mBack = node;
    }
  }

  void pushBack(const T &val) {
    auto node(new Node<T>(val));

    if (mBack) {
      node->mPrev = mBack;
      mBack->mNext = node;

      mBack = node;
    } else {
      mFront = node;
      mBack = node;
    }
  }

  void popFront() {
    if (mFront) {
      auto next(mFront->mNext);
      delete mFront;

      if (next) {
        next->mPrev = nullptr;
        mFront = next;
      } else {
        mFront = mBack = nullptr;
      }
    }
  }

  void popBack() {
    if (mBack) {
      auto prev(mBack->mPrev);
      delete mBack;

      if (prev) {
        prev->mNext = nullptr;
        mBack = prev;
      } else {
        mFront = mBack = nullptr;
      }
    }
  }

  void erase(const T &val) {
    auto node(mFront);

    while (node) {
      if (node->mValue == val) {
        if (node->mPrev)
          node->mPrev->mNext = node->mNext;
        else
          mFront = node->mNext;

        if (node->mNext)
          node->mNext->mPrev = node->mPrev;
        else
          mBack = node->mPrev;

        delete node;

        break;
      }

      node = node->mNext;
    }
  }

  void print() {
    auto node(mFront);

    size_t i(0);
    while (node) {
      printf(i++ ? " %d" : "%d", node->mValue);

      node = node->mNext;
    }

    printf("\n");
  }
};

int main() {
  List<int> list;

  int n;
  scanf("%d", &n);

  for (size_t i(0); i < n; ++i) {
    char command[16];
    scanf("%s", command);

    if (!strcmp(command, "insert")) {
      int x;
      scanf("%d", &x);

      list.pushFront(x);
    } else if (!strcmp(command, "delete")) {
      int x;
      scanf("%d", &x);

      list.erase(x);
    } else if (!strcmp(command, "deleteFirst")) {
      list.popFront();
    } else if (!strcmp(command, "deleteLast")) {
      list.popBack();
    }
  }

  list.print();

  while (true) {
  }

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// use printf, scanf instead of cout, cin.

using namespace std;

template <typename T> struct Node {
  Node(const T &value) : mValue(value), mNext(nullptr), mPrev(nullptr) {}

  T mValue;

  Node<T> *mNext;
  Node<T> *mPrev;
};

template <typename T> class List {
private:
  Node<T> *mFront;
  Node<T> *mBack;

public:
  List() : mFront(nullptr), mBack(nullptr) {}
  ~List() {
    while (mFront)
      popFront();
  }

  void pushFront(const T &val) {
    auto node(new Node<T>(val));

    if (mFront) {
      node->mNext = mFront;
      mFront->mPrev = node;

      mFront = node;
    } else {
      mFront = node;
      mBack = node;
    }
  }

  void pushBack(const T &val) {
    auto node(new Node<T>(val));

    if (mBack) {
      node->mPrev = mBack;
      mBack->mNext = node;

      mBack = node;
    } else {
      mFront = node;
      mBack = node;
    }
  }

  void popFront() {
    if (mFront) {
      auto next(mFront->mNext);
      delete mFront;

      if (next) {
        next->mPrev = nullptr;
        mFront = next;
      } else {
        mFront = mBack = nullptr;
      }
    }
  }

  void popBack() {
    if (mBack) {
      auto prev(mBack->mPrev);
      delete mBack;

      if (prev) {
        prev->mNext = nullptr;
        mBack = prev;
      } else {
        mFront = mBack = nullptr;
      }
    }
  }

  void erase(const T &val) {
    auto node(mFront);

    while (node) {
      if (node->mValue == val) {
        if (node->mPrev)
          node->mPrev->mNext = node->mNext;
        else
          mFront = node->mNext;

        if (node->mNext)
          node->mNext->mPrev = node->mPrev;
        else
          mBack = node->mPrev;

        delete node;

        break;
      }

      node = node->mNext;
    }
  }

  void print() {
    auto node(mFront);

    size_t i(0);
    while (node) {
      printf(i++ ? " %d" : "%d", node->mValue);

      node = node->mNext;
    }

    printf("\n");
  }
};

int main() {
  List<int> list;

  int n;
  scanf("%d", &n);

  for (size_t i(0); i < n; ++i) {
    char command[16];
    scanf("%s", command);

    if (!strcmp(command, "insert")) {
      int x;
      scanf("%d", &x);

      list.pushFront(x);
    } else if (!strcmp(command, "delete")) {
      int x;
      scanf("%d", &x);

      list.erase(x);
    } else if (!strcmp(command, "deleteFirst")) {
      list.popFront();
    } else if (!strcmp(command, "deleteLast")) {
      list.popBack();
    }
  }

  list.print();

  return 0;
}