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

#include <cstdio>
#include <cstdlib>
#include <cstring>

struct Node {
  int key;
  Node *next, *prev;
};

Node *nil;

Node *listSearch(int key) {
  Node *cur = nil->next;
  while (cur != nil && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void printList() {
  Node *cur = nil->next;
  int isf = 0;
  while (true) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

void deleteNode(Node *t) {
  if (t == nil)
    return;
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

int main() {
  int key, n;
  int size = 0;
  char com[20];
  int np = 0, nd = 0;
  scanf("%d", &n);
  init();
  for (int i = 0; i < n; ++i) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i') {
      insert(key);
      ++np;
      ++size;
    } else if (com[0] == 'd') {
      if (strlen(com) > 6) {
        if (com[6] == 'F')
          deleteFirst();
        else if (com[6] == 'L')
          deleteLast();
      } else {
        deleteKey(key);
        ++nd;
      }
      --size;
    }
  }

  printList();

  return 0;
}


Error: TLE

Fixed submission:

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

namespace tom {
class list {
private:
  struct node {
    int key = 0;
    node *next = NULL;
    node *prev = NULL;
  };

  node *_head = NULL;
  int _cnt = 0;

public:
  list() {
    _head = new node;
    _head->next = _head;
    _head->prev = _head;
  }
  ~list() {}

  bool insert(int input) {
    ++_cnt;
    node *newnode = new node;
    if (newnode == NULL)
      return false;
    newnode->key = input;
    /*
    node* itr;
    for(itr = _head; itr->next != _head; itr = itr->next);
    itr->next = newnode;
    newnode->next = _head;
    newnode->prev = itr;
    _head->prev = newnode;
    */
    newnode->next = _head->next;
    newnode->prev = _head;
    _head->next->prev = newnode;
    _head->next = newnode;
    if (_cnt == 0) {
      _head->prev = newnode;
    }
    return true;
  }

  bool deleteX(int key) {
    node *itr;
    for (itr = _head->next; itr != _head; itr = itr->next) {
      if (itr->key == key) {
        itr->prev->next = itr->next;
        itr->next->prev = itr->prev;
        delete itr;
        --_cnt;
        return true;
      }
    }
    return false;
  }

  bool deleteFirst() {
    node *itr = _head->next;
    if (itr != _head) {
      itr->prev->next = itr->next;
      itr->next->prev = itr->prev;
      delete itr;
      --_cnt;
      return true;
    }

    return false;
  }
  bool deleteLast() {
    node *itr = _head->prev;
    if (itr != _head) {
      itr->prev->next = itr->next;
      itr->next->prev = itr->prev;
      delete itr;
      --_cnt;
      return true;
    }
    return false;
  }

  ///*
  void showlist() {
    if (_cnt > 0) {
      node *itr;
      for (itr = _head; itr->next != _head; itr = itr->next) {
        cout << itr->next->key;
        if (itr->next->next != _head)
          cout << " ";
      }
    }
    // else
    // cout << "There is not node.";
    cout << endl;
  }
  //*/
};
} // namespace tom

void order(string command, tom::list &list) {
  if (command == "insert") {
    int in;
    cin >> in;
    list.insert(in);
  } else if (command == "delete") {
    int x;
    cin >> x;
    list.deleteX(x);
  } else if (command == "deleteFirst") {
    list.deleteFirst();
  } else if (command == "deleteLast") {
    list.deleteLast();
  } else {
    cout << "Such a command does not exist.";
  }
}

int main() {
  tom::list _list;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string work;
    cin >> work;
    order(work, _list);
  }

  _list.showlist();

  return 0;
}