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

#include <iostream>
#define SIZE 2000000
using namespace std;
struct dList {
  int head, tail;
  int list[SIZE];
  void init() { head = -1; }
  void insertX(int x) {
    if (head == -1) {
      head = SIZE;
      tail = SIZE - 1;
    }
    head--;
    list[head] = x;
  }
  void deleteFirst() {
    if (head == tail) {
      head = -1;
    } else {
      while (list[head + 1] < 0) {
        head++;
      }
      head++;
    }
  }
  void deleteLast() {
    if (head == tail) {
      deleteFirst();
    } else {
      while (list[tail - 1] < 0) {
        tail--;
      }
      tail--;
    }
  }
  void deleteX(int x) {
    bool isExist = false;
    if (list[head] == x) {
      deleteFirst();
    } else {
      for (int i = head + 1; i < tail; i++) {
        if (list[i] == x) {
          list[i] = -1;
          isExist = true;
          break;
        }
      }
      if (!isExist) {
        if (list[tail] == x)
          deleteLast();
      }
    }
  }
};

int main() {
  int n, value;
  cin >> n;
  string op;
  dList dlist;
  dlist.init();
  for (int i = 0; i < n; i++) {
    cin >> op;
    if (op == "deleteFirst") {
      dlist.deleteFirst();
    } else if (op == "deleteLast") {
      dlist.deleteLast();
    } else {
      cin >> value;
      if (op == "insert") {
        dlist.insertX(value);
      } else if (op == "delete") {
        dlist.deleteX(value);
      } else {
        cout << "ERROR(operation " << op << " DOESNOT exist" << endl;
      }
    }
  }

  for (int i = dlist.head; i <= dlist.tail; i++) {
    if (dlist.list[i] >= 0) {
      if (i == dlist.tail) {
        cout << dlist.list[i] << endl;
      } else {
        cout << dlist.list[i] << ' ';
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#define SIZE 2000000
using namespace std;

struct dList {
  int head, tail;
  int list[SIZE];
  int next[SIZE];
  void init() {
    head = -1;
    tail = SIZE - 1;
    next[tail] = -1;
  }

  int searchDeb(int x, int array[]) {
    for (int i = 0; i < SIZE; i++) {
      if (array[i] == x)
        return i;
    }
    return -2;
  }

  int search(int x) {
    int i = head;
    while (i != -1) {
      if (list[i] == x) {
        // cout <<"list[i]:"<<i<<' '<<list[i]<<endl;
        return i;
      } else {
        i = next[i];
      }
    }
    return -2;
  }

  void insertX(int x) {
    int tmp;
    if (head == 0) {
      cout << "ERROR(OVER FLOW)" << endl;
      // exit(1);
    } else if (head == -1) {
      head = SIZE;
      tmp = -1;
    } else {
      tmp = head;
    }
    head--;
    list[head] = x;
    next[head] = tmp;
  }

  void deleteFirst() { head = next[head]; }
  void deleteLast() {
    if (head == tail) {
      deleteFirst();
    } else {
      int numA = searchDeb(tail, next); //<-need to fix
      next[numA] = -1;
      tail = numA;
    }
  }
  void deleteX(int x) {
    if (head == tail) {
    }
    if (search(x) == tail) {
      deleteLast();
    } else if (search(x) == head) {
      deleteFirst();
    } else {
      int numA = search(x);
      int numB = searchDeb(numA, next);
      next[numB] = next[numA];
    }
  }
};

int main() {
  int n, value;
  string operation;
  dList dlist;
  dlist.init();
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> operation;
    if (operation == "deleteFirst") {
      dlist.deleteFirst();
    } else if (operation == "deleteLast") {
      dlist.deleteLast();
    } else {
      cin >> value;
      if (operation == "delete") {
        dlist.deleteX(value);
      } else if (operation == "insert") {
        dlist.insertX(value);
      } else {
        cout << "ERROR(OPERATION " << operation << " DOESNOT EXIST)" << endl;
        // exit(1);
      }
    }
  }

  int i = dlist.head;
  while (i != -1) {
    if (i == dlist.tail) {
      cout << dlist.list[i] << endl;
      ;
    } else {
      cout << dlist.list[i] << ' ';
    }
    i = dlist.next[i];
  }
  return 0;
}