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

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

#define BUF_SIZE 1024

struct Node {
  int data;
  Node *prev;
  Node *next;
};

class List {
private:
  Node *start;

public:
  List() {
    start = new Node();
    start->data = 0;
    start->prev = start;
    start->next = start;
  };
  ~List() {
    Node *tmp1 = start->next;
    Node *tmp2;
    while (tmp1 != start) {
      tmp2 = tmp1->next;
      delete tmp1;
      tmp1 = tmp2;
    }
    delete start;
  };
  void insertx(int x) {
    Node *tmp = start->next;
    start->next = new Node();
    start->next->data = x;
    start->next->prev = start;
    start->next->next = tmp;
    tmp->prev = start->next;
  };
  void deletex(int x) {
    Node *tmp = start->next;
    while (tmp != start && tmp->data != x) {
      tmp = tmp->next;
    }
    if (tmp != start) {
      tmp->next->prev = tmp->prev;
      tmp->prev->next = tmp->next;
      delete tmp;
    }
  };
  void deleteFirst() {
    Node *tmp = start->next;
    start->next = start->next->next;
    start->next->prev = start;
    delete tmp;
  };
  void deleteLast() {
    Node *tmp = start->prev;
    start->prev = start->prev->prev;
    start->prev->next = start;
    delete tmp;
  };
  void print() {
    Node *tmp = start->next;
    while (tmp != start) {
      printf("%d", tmp->data);
      tmp = tmp->next;
      if (tmp != start)
        printf(" ");
    }
    printf("\\n");
  }
};

int main(void) {
  char buf[BUF_SIZE];
  int n = atoi(fgets(buf, BUF_SIZE, stdin));
  List list;
  for (int i = 0; i < n; ++i) {
    fgets(buf, BUF_SIZE, stdin);
    if (buf[6] == 'F')
      list.deleteFirst();
    else if (buf[6] == 'L')
      list.deleteLast();
    else {
      int x = 0;
      for (int j = 0; buf[j] != '\\0'; ++j)
        if (buf[j] >= '0' && buf[j] <= '9')
          x = 10 * x + (buf[j] - '0');
      if (buf[0] == 'i')
        list.insertx(x);
      else
        list.deletex(x);
    }
  }
  list.print();
  return 0;
}

Error: TLE

Fixed submission:

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

struct Node {
  int data;
  Node *prev;
  Node *next;
};

class DLList {
private:
  Node *start;

public:
  DLList() {
    start = new Node();
    start->data = 0;
    start->prev = start;
    start->next = start;
  };
  ~DLList() {
    Node *tmp1, *tmp2;
    tmp1 = start->next;
    while (tmp1 != start) {
      tmp2 = tmp1->next;
      delete tmp1;
      tmp1 = tmp2;
    }
    delete start;
  };
  void insertx(int x) {
    Node *tmp = start->next;
    start->next = new Node();
    start->next->data = x;
    start->next->prev = start;
    start->next->next = tmp;
    tmp->prev = start->next;
  };
  void deletex(int x) {
    Node *tmp = start->next;
    while (tmp != start && tmp->data != x) {
      tmp = tmp->next;
    }
    if (tmp != start) {
      tmp->next->prev = tmp->prev;
      tmp->prev->next = tmp->next;
      delete tmp;
    }
  };
  void deleteFirst() {
    Node *tmp = start->next;
    start->next = start->next->next;
    start->next->prev = start;
    delete tmp;
  };
  void deleteLast() {
    Node *tmp = start->prev;
    start->prev = start->prev->prev;
    start->prev->next = start;
    delete tmp;
  };
  void print() {
    Node *tmp = start->next;
    while (tmp != start) {
      cout << tmp->data;
      tmp = tmp->next;
      if (tmp != start)
        cout << " ";
    }
    cout << endl;
  }
};

int main(void) {
  int n;
  cin >> n;
  DLList list;
  for (int i = 0; i < n; ++i) {
    string str;
    cin >> str;
    if (str == "deleteFirst")
      list.deleteFirst();
    else if (str == "deleteLast")
      list.deleteLast();
    else {
      int x;
      cin >> x;
      if (str == "insert")
        list.insertx(x);
      else
        list.deletex(x);
    }
  }
  list.print();
  return 0;
}