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

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

void insert(std::list<int> &lst);
void del(std::list<int> &lst);
void deleteFirst(std::list<int> &lst);
void deleteLast(std::list<int> &lst);
void command(std::list<int> &lst, std::string str);
void print(std::list<int> &lst);

int main() {
  int n;
  std::string str;
  std::list<int> lst;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> str;
    command(lst, str);
  }
  print(lst);
}

void insert(std::list<int> &lst) {
  int x;
  std::cin >> x;
  lst.push_front(x);
}
void del(std::list<int> &lst) {
  std::list<int>::iterator itr;
  int x;
  std::cin >> x;
  for (itr = lst.begin(); itr != lst.end(); ++itr) {
    if (*itr == x) {
      lst.erase(itr);
      break;
    }
  }
}
void deleteFirst(std::list<int> &lst) { lst.pop_front(); }
void deleteLast(std::list<int> &lst) { lst.pop_back(); }
void command(std::list<int> &lst, std::string str) {
  if (str == "insert") {
    insert(lst);
  } else if (str == "delete") {
    del(lst);
  } else if (str == "deleteFirst") {
    deleteFirst(lst);
  } else {
    deleteLast(lst);
  }
}

void print(std::list<int> &lst) {
  std::list<int>::iterator itr;
  std::list<int>::iterator itr2 = lst.end();
  itr2--;
  for (itr = lst.begin(); itr != lst.end(); ++itr) {
    std::cout << *itr;
    if (itr != itr2) {
      std::cout << " ";
    }
  }
  std::cout << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

struct List {
  int value[1000000];
  int next[1000000];
  int prev[1000000];
  int head = -1;
  int tail = -1;
  int idx = 0;

  void insertion(int x) {
    next[idx] = head;
    prev[head] = idx;
    if (tail == -1) {
      tail = idx;
    }
    value[idx] = x;
    head = idx;
    idx++;
  }

  int serch(int x) {
    int j = head;
    while (j != -1) {
      if (value[j] == x) {
        return j;
      }
      j = next[j];
    }
    return -1;
  }

  void deleteFirst() {
    if (next[head] == -1) {
      head = -1;
      tail = -1;
    } else {
      head = next[head];
      prev[head] = -1;
    }
  }

  void deleteLast() {
    if (prev[tail] == -1) {
      head = -1;
      tail = -1;
    } else {
      tail = prev[tail];
      next[tail] = -1;
    }
  }

  void del(int x) {
    int s = serch(x);
    if (s != -1) {
      if (s == head) {
        deleteFirst();
      } else if (s == tail) {
        deleteLast();
      } else {
        int p = prev[s];
        int n = next[s];
        next[p] = n;
        prev[n] = p;
      }
    }
  }

  void command(std::string str) {
    if (str == "insert") {
      int x;
      std::cin >> x;
      insertion(x);
    } else if (str == "delete") {
      int x;
      std::cin >> x;
      del(x);
    } else if (str == "deleteFirst") {
      deleteFirst();
    } else {
      deleteLast();
    }
  }

  void print() {
    int k = head;
    while (k != -1) {
      std::cout << value[k];
      k = next[k];
      if (k != -1) {
        std::cout << " ";
      }
    }
    std::cout << "\\n";
  }
};

int main() {
  List lst;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::string str;
    std::cin >> str;
    lst.command(str);
  }
  lst.print();
  return 0;
}