Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Node {
  int key;
  Node *next;
  Node *pre;
};

struct List {
  Node *first;

  void init() {
    first = (Node *)malloc(sizeof(Node));
    first->key = 0;
    first->next = first;
    first->pre = first;
  }

  void insert(int x) {
    Node *nextnode = (Node *)malloc(sizeof(Node));
    nextnode->key = x;
    nextnode->next = first->next;
    first->next->pre = nextnode;
    first->next = nextnode;
    nextnode->pre = first;
  }

  void delete_x(int x) {
    for (Node *p = first->next; p != first; p = p->next) {
      if (p->key == x) {
        p->next->pre = p->pre;
        p->pre->next = p->next;
        free(p);
        return;
      }
    }
  }

  void deleteFirst() {
    Node *p = first->next;
    p->next->pre = p->pre;
    first->next = p->next;
    free(p);
  }

  void deleteLast() {
    Node *p;
    for (p = first; p->next != first; p = p->next)
      ;
    p->pre->next = first;
  }

  void print_list() {
    bool flg = false;
    for (Node *p = first->next; p != first; p = p->next) {
      if (flg)
        cout << " ";
      cout << p->key;
      flg = true;
    }
    cout << endl;
  }
};

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  List list;
  list.init();
  string s;
  int k;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> k;
      list.insert(k);
    } else if (s == "delete") {
      cin >> k;
      list.delete_x(k);
    } else if (s == "deleteFirst") {
      list.deleteFirst();
    } else {
      list.deleteLast();
    }
  }
  list.print_list();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Node {
  int key;
  Node *next;
  Node *pre;
};

struct List {
  Node *first;

  void init() {
    first = (Node *)malloc(sizeof(Node));
    first->key = 0;
    first->next = first;
    first->pre = first;
  }

  void insert(int x) {
    Node *nextnode = (Node *)malloc(sizeof(Node));
    nextnode->key = x;
    nextnode->next = first->next;
    first->next->pre = nextnode;
    first->next = nextnode;
    nextnode->pre = first;
  }

  void delete_x(int x) {
    for (Node *p = first->next; p != first; p = p->next) {
      if (p->key == x) {
        p->next->pre = p->pre;
        p->pre->next = p->next;
        free(p);
        return;
      }
    }
  }

  void deleteFirst() {
    Node *p = first->next;
    p->next->pre = p->pre;
    first->next = p->next;
    free(p);
  }

  void deleteLast() {
    Node *p = first->pre;
    //        for(p = first; p->next != first; p=p->next);
    p->pre->next = p->next;
    p->next->pre = p->pre;
  }

  void print_list() {
    bool flg = false;
    for (Node *p = first->next; p != first; p = p->next) {
      if (flg)
        cout << " ";
      cout << p->key;
      flg = true;
    }
    cout << endl;
  }
};

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  List list;
  list.init();
  string s;
  int k;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> k;
      list.insert(k);
    } else if (s == "delete") {
      cin >> k;
      list.delete_x(k);
    } else if (s == "deleteFirst") {
      list.deleteFirst();
    } else {
      list.deleteLast();
    }
  }
  list.print_list();
}