Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

class Node {
private:
  int data;
  Node *next;
  Node *prev;

public:
  Node() {
    next = NULL;
    prev = NULL;
  }
  void setData(int data) { this->data = data; }
  void setNext(Node *next) { this->next = next; }
  void setPrev(Node *prev) { this->prev = prev; }

  int getData() { return data; }
  Node *getNext() { return next; }
  Node *getPrev() { return prev; }
};

class List {
private:
  Node *head;
  Node *tail;

public:
  List() {
    head = new Node;
    tail = new Node;
    head->setPrev(tail); // headの後ろがtail
    tail->setNext(head);
  }
  void insert(int x) {
    Node *newNode;
    newNode = new Node;
    newNode->setData(x);
    newNode->setNext(head);
    newNode->setPrev(head->getPrev());
    head->getPrev()->setNext(newNode);
    head->setPrev(newNode);
  }
  void deleteNode(int x) {
    tail->setData(x);
    Node *i = head->getPrev();
    while (i->getData() != x)
      i = i->getPrev();
    if (i != tail) {
      i->getNext()->setPrev(i->getPrev());
      i->getPrev()->setNext(i->getNext());
      delete i;
    }
  }
  void deleteFirst() {
    Node *tmp = head->getPrev();
    tmp->getPrev()->setNext(head);
    head->setPrev(tmp->getPrev());
    delete tmp;
  }
  void deleteLast() {
    Node *tmp = tail->getNext();
    tmp->getNext()->setPrev(tail);
    tail->setPrev(tmp->getNext());
    delete tmp;
  }
  void showList() {
    Node *i = head->getPrev()->getPrev();
    cout << head->getPrev()->getData();
    while (i != tail) {
      cout << " " << i->getData();
      i = i->getPrev();
    }
    cout << endl;
  }
  ~List() {
    Node *tmp = head->getPrev();
    while (tmp != tail) {
      delete tmp->getNext();
      tmp = tmp->getPrev();
    }
    delete tmp;
  }
};

int main() {
  List list;
  unsigned n;
  string command; // コマンド
  int x;          // 値
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    } else if (command == "insert") {
      cin >> x;
      list.insert(x);
    } else if (command == "delete") {
      cin >> x;
      list.deleteNode(x);
    } else {
      cout << "Unknown command" << endl;
    }
  }

  list.showList();

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;

class Node {
private:
  int data;
  Node *next;
  Node *prev;

public:
  Node() {
    next = NULL;
    prev = NULL;
  }
  void setData(int data) { this->data = data; }
  void setNext(Node *next) { this->next = next; }
  void setPrev(Node *prev) { this->prev = prev; }

  int getData() { return data; }
  Node *getNext() { return next; }
  Node *getPrev() { return prev; }
};

class List {
private:
  Node *head;
  Node *tail;

public:
  List() {
    head = new Node;
    tail = new Node;
    head->setPrev(tail); // headの後ろがtail
    tail->setNext(head);
  }
  void insert(int x) {
    Node *newNode;
    newNode = new Node;
    newNode->setData(x);
    newNode->setNext(head);
    newNode->setPrev(head->getPrev());
    head->getPrev()->setNext(newNode);
    head->setPrev(newNode);
  }
  void deleteNode(int x) {
    tail->setData(x);
    Node *i = head->getPrev();
    while (i->getData() != x)
      i = i->getPrev();
    if (i != tail) {
      i->getNext()->setPrev(i->getPrev());
      i->getPrev()->setNext(i->getNext());
      delete i;
    }
  }
  void deleteFirst() {
    Node *tmp = head->getPrev();
    tmp->getPrev()->setNext(head);
    head->setPrev(tmp->getPrev());
    delete tmp;
  }
  void deleteLast() {
    Node *tmp = tail->getNext();
    tmp->getNext()->setPrev(tail);
    tail->setNext(tmp->getNext());
    delete tmp;
  }
  void showList() {
    Node *i = head->getPrev()->getPrev();
    cout << head->getPrev()->getData();
    while (i != tail) {
      cout << " " << i->getData();
      i = i->getPrev();
    }
    cout << endl;
  }
  ~List() {
    Node *tmp = head->getPrev();
    while (tmp != tail) {
      delete tmp->getNext();
      tmp = tmp->getPrev();
    }
    delete tmp;
  }
};

int main() {
  List list;
  unsigned n;
  string command; // コマンド
  int x;          // 値
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    } else if (command == "insert") {
      cin >> x;
      list.insert(x);
    } else if (command == "delete") {
      cin >> x;
      list.deleteNode(x);
    } else {
      cout << "Unknown command" << endl;
    }
  }

  list.showList();

  return 0;
}