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


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

struct t_node {
  int x;
  t_node *prev;
  t_node *next;
};

int main() {
  t_node *head = NULL, *tail = NULL;
  char in[16];
  int n;
  cin >> n;
  while (n--) {
    cin >> in;
    if (in[0] == 'i') {
      int k;
      cin >> k;
      //            t_node *newNode = new t_node;
      t_node *newNode = (t_node *)malloc(sizeof(t_node));
      newNode->x = k;
      if (head == NULL) {
        head = newNode;
        head->prev = head->next = NULL;
      } else {
        newNode->next = head;
        newNode->prev = NULL;
        head->prev = newNode;
        head = newNode;
      }
      if (tail == NULL)
        tail = newNode;
    } else if (in[6] == 'F') {
      t_node *del = head;
      if (head == tail)
        tail = NULL;
      head = head->next;
      if (head != NULL)
        head->prev = NULL;
      //			delete del;
      free(del);
    } else if (in[6] == 'L') {
      t_node *del = tail;
      if (tail == head)
        head = NULL;
      tail = tail->prev;
      if (tail != NULL)
        tail->next = NULL;
      //			delete del;
      free(del);
    } else {
      int k;
      cin >> k;
      t_node *cur = head;
      while (cur != NULL) {
        if (cur->x == k) {
          if (cur->prev != NULL)
            cur->prev->next = cur->next;
          if (cur->next != NULL)
            cur->next->prev = cur->prev;
          if (cur == head)
            head = cur->next;
          if (cur == tail)
            tail = cur->prev;
          //                    delete cur;
          free(cur);
          break;
        }
        cur = cur->next;
      }
    }
  }
  if (head != NULL) {
    cout << head->x;
    head = head->next;
  }
  while (head != NULL) {
    t_node *cur = head;
    head = head->next;
    cout << " " << cur->x;
    //        delete cur;
    free(cur);
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

struct t_node {
  int x;
  t_node *prev;
  t_node *next;
};

int main() {
  t_node *head = NULL, *tail = NULL;
  string in;
  int n;
  cin >> n;
  while (n--) {
    cin >> in;
    if (in[0] == 'i') {
      int k;
      cin >> k;
      //            t_node *newNode = new t_node;
      t_node *newNode = (t_node *)malloc(sizeof(t_node));
      newNode->x = k;
      if (head == NULL) {
        head = newNode;
        head->prev = head->next = NULL;
      } else {
        newNode->next = head;
        newNode->prev = NULL;
        head->prev = newNode;
        head = newNode;
      }
      if (tail == NULL)
        tail = newNode;
    } else if (in == "delete") {
      int k;
      cin >> k;
      t_node *cur = head;
      while (cur != NULL) {
        if (cur->x == k) {
          if (cur->prev != NULL)
            cur->prev->next = cur->next;
          if (cur->next != NULL)
            cur->next->prev = cur->prev;
          if (cur == head)
            head = cur->next;
          if (cur == tail)
            tail = cur->prev;
          //                    delete cur;
          free(cur);
          break;
        }
        cur = cur->next;
      }
    } else if (in == "deleteFirst") {
      t_node *del = head;
      if (head == tail)
        tail = NULL;
      head = head->next;
      if (head != NULL)
        head->prev = NULL;
      //			delete del;
      free(del);
    } else if (in == "deleteLast") {
      t_node *del = tail;
      if (tail == head)
        head = NULL;
      tail = tail->prev;
      if (tail != NULL)
        tail->next = NULL;
      //			delete del;
      free(del);
    }
  }
  if (head != NULL) {
    cout << head->x;
    head = head->next;
  }
  while (head != NULL) {
    t_node *cur = head;
    head = head->next;
    cout << " " << cur->x;
    //        delete cur;
    free(cur);
  }
  cout << endl;
  return 0;
}