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

#include <climits>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

typedef struct list {
  int v;
  struct list *prev;
  struct list *next;
} LIST;

LIST *ListFirst = NULL;
LIST *ListLast = NULL;

LIST *search(int x) {
  LIST *l;
  if (ListFirst == NULL)
    return NULL;

  l = ListFirst;
  for (;;) {
    if (l->v == x)
      return l;
    if (l->next == NULL)
      return NULL;
    else
      l = l->next;
  }
}

void insert(int x) {
  LIST *l;
  l = (LIST *)malloc(sizeof(LIST));
  l->v = x;
  l->prev = NULL;
  l->next = ListFirst;
  if (ListFirst)
    ListFirst->prev = l;
  ListFirst = l;
  if (ListLast == NULL) {
    ListLast = l;
  }
}

void deleteList(LIST *l) {
  if (l->prev && l->next) {
    l->prev->next = l->next;
    l->next->prev = l->prev;
  } else if (l->prev == NULL && l->next) {
    l->next->prev = NULL;
    ListFirst = l->next;
  } else if (l->next == NULL && l->prev) {
    l->prev->next = NULL;
    ListLast = l->prev;
  } else {
    ListFirst = ListLast = NULL;
  }
  free(l);
}

void delete_l(int x) {
  LIST *l;
  l = search(x);
  if (l != NULL) {
    deleteList(l);
  }
}

void deleteFirst() {
  // printf("%p %p %p\\n",ListFirst,ListFirst->prev,ListFirst->next);
  deleteList(ListFirst);
}

void deleteLast() {
  // printf("%p %p %p\\n",ListLast,ListLast->prev,ListLast->next);
  deleteList(ListLast);
}

void outputList(LIST *l) {
  while (l != NULL) {
    printf("%d", l->v);
    l = l->next;
    if (l)
      putchar(' ');
    else
      putchar('\\n');
  }
}

int main() {
  int n, v;
  char cmd[12];

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

  for (int i = 0; i < n; i++) {
    scanf("%s ", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d\\n", &v);
      insert(v);
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "delete") == 0) {
      scanf("%d\\n", &v);
      delete_l(v);
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "deleteFirst") == 0) {
      deleteFirst();
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "deleteLast") == 0) {
      deleteLast();
      // outputList(ListFirst);
    }
  }

  outputList(ListFirst);
}


Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

typedef struct list {
  int v;
  struct list *prev;
  struct list *next;
} LIST;

LIST *ListFirst = NULL;
LIST *ListLast = NULL;

LIST *search(int x) {
  LIST *l;
  if (ListFirst == NULL)
    return NULL;

  l = ListFirst;
  for (;;) {
    if (l->v == x)
      return l;
    if (l->next == NULL)
      return NULL;
    else
      l = l->next;
  }
}

void insert(int x) {
  LIST *l;
  l = (LIST *)malloc(sizeof(LIST));
  l->v = x;
  l->prev = NULL;
  l->next = ListFirst;
  if (ListFirst)
    ListFirst->prev = l;
  ListFirst = l;
  if (ListLast == NULL) {
    ListLast = l;
  }
}

void deleteList(LIST *l) {
  if (l->prev && l->next) {
    l->prev->next = l->next;
    l->next->prev = l->prev;
  } else if (l->prev == NULL && l->next) {
    l->next->prev = NULL;
    ListFirst = l->next;
  } else if (l->next == NULL && l->prev) {
    l->prev->next = NULL;
    ListLast = l->prev;
  }
  free(l);
}

void delete_l(int x) {
  LIST *l;
  l = search(x);
  if (l != NULL) {
    deleteList(l);
  }
}

void deleteFirst() { deleteList(ListFirst); }

void deleteLast() { deleteList(ListLast); }

void outputList(LIST *l) {
  while (l != NULL) {
    printf("%d", l->v);
    l = l->next;
    if (l)
      putchar(' ');
    else
      putchar('\\n');
  }
}

int main() {
  int n, v;
  char cmd[12];

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

  for (int i = 0; i < n; i++) {
    scanf("%s ", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d\\n", &v);
      insert(v);
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "delete") == 0) {
      scanf("%d\\n", &v);
      delete_l(v);
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "deleteFirst") == 0) {
      deleteFirst();
      // outputList(ListFirst);
    }
    if (strcmp(cmd, "deleteLast") == 0) {
      deleteLast();
      // outputList(ListFirst);
    }
  }

  outputList(ListFirst);
}