Problem p02265 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

struct node {
  int data;
  struct node *next, *prev;
};

class list {
public:
  node *head, *tail;
  list() : head(NULL), tail(NULL){};
  void insert(int x) {
    node *c = new node;
    c->data = x;
    c->prev = NULL;
    c->next = head;
    if (head == NULL) {
      head = tail = c;
    } else {
      head->prev = c;
      head = c;
    }
  }
  void deleteFirst() {
    node *c = head;
    head = head->next;
    if (head)
      head->prev = NULL;
    else
      tail = NULL;
    delete c;
  }
  void deleteLast() {
    node *c = tail;
    tail = tail->prev;
    if (tail)
      tail->next = NULL;
    else
      head = NULL;
    delete c;
  }
  void deletex(int x) {
    node *c;
    for (c = head; c != NULL && c->data != x; c = c->next)
      ;
    if (c == NULL)
      return;
    if (c->prev == NULL)
      deleteFirst();
    else if (c->next == NULL)
      deleteLast();
    else {
      c->prev->next = c->next;
      c->next->prev = c->prev;
      delete c;
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string com;
  int x;
  list li;
  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> x;
      li.insert(x);
    } else if (com[6] == 'F') {
      li.deleteFirst();
    } else if (com[6] == 'L') {
      li.deleteLast();
    } else {
      cin >> x;
      li.deletex(x);
    }
  }
  bool first = true;
  for (node *c = li.head; c; c = c->next) {
    if (!first)
      cout << ' ';
    else
      first = false;
    cout << c->data;
  }
  cout << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

struct node {
  int data;
  struct node *next, *prev;
};

class list {
public:
  node *head, *tail;
  list() : head(NULL), tail(NULL){};
  void insert(int x) {
    node *c = new node;
    c->data = x;
    c->prev = NULL;
    c->next = head;
    if (head == NULL) {
      head = tail = c;
    } else {
      head->prev = c;
      head = c;
    }
  }
  void deleteFirst() {
    node *c = head;
    head = head->next;
    head->prev = NULL;
    delete c;
    if (head == NULL)
      tail = NULL;
  }
  void deleteLast() {
    node *c = tail;
    tail = tail->prev;
    tail->next = NULL;
    delete c;
    if (tail == NULL)
      head = NULL;
  }
  void deletex(int x) {
    node *c;
    for (c = head; c != NULL && c->data != x; c = c->next)
      ;
    if (c == NULL)
      return;
    if (c->prev == NULL)
      deleteFirst();
    else if (c->next == NULL)
      deleteLast();
    else {
      c->prev->next = c->next;
      c->next->prev = c->prev;
      delete c;
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string com;
  int x;
  list li;
  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> x;
      li.insert(x);
    } else if (com[6] == 'F') {
      li.deleteFirst();
    } else if (com[6] == 'L') {
      li.deleteLast();
    } else {
      cin >> x;
      li.deletex(x);
    }
  }
  bool first = true;
  for (node *c = li.head; c; c = c->next) {
    if (!first)
      cout << ' ';
    else
      first = false;
    cout << c->data;
  }
  cout << '\\n';
  return 0;
}