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

#include <iostream>
#include <string>

using namespace std;

typedef struct LinkedList LinkedList;

struct LinkedList {
  LinkedList *front = NULL;
  LinkedList *back = NULL;
  int key = -1;
};

int main() {
  int N, key;
  string command;
  LinkedList *first = NULL;
  LinkedList *last = NULL;
  int index = 0;
  LinkedList *temp;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> key;
      LinkedList *insert_temp = new LinkedList();
      insert_temp->key = key;
      if (first != NULL) {
        first->front = insert_temp;

        insert_temp->back = first;
        first = insert_temp;

      } else {
        first = insert_temp;
        last = insert_temp;
      }
      index++;
    } else if (command == "delete") {
      cin >> key;
      temp = first;
      while (true) {
        if (temp->key == key) {
          if (temp->back == NULL) {
            last = temp->front;
            last->back = NULL;
          } else if (temp->front == NULL) {
            first = temp->back;
            first->front = NULL;
          } else {
            temp->front->back = temp->back;
            temp->back->front = temp->front;
          }
          break;
        }
        if (temp->back == NULL)
          break;
        else
          temp = temp->back;
      }

    } else if (first != NULL) {
      if (command == "deleteFirst") {
        first = first->back;
        if (first != NULL)
          first->front = NULL;
        else
          last = NULL;
      } else if (command == "deleteLast") {
        last = last->front;
        if (last != NULL)
          last->back = NULL;
        else
          first = NULL;
      }
    }
  }

  while (true) {
    cout << first->key;
    if (first->back == NULL) {
      break;
    } else {
      cout << " ";
    }
    first = first->back;
  }
  cout << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

typedef struct LinkedList LinkedList;

struct LinkedList {
  LinkedList *front = NULL;
  LinkedList *back = NULL;
  int key = -1;
  bool is_first = false;
  bool is_last = false;
};

int main() {
  int N, key;
  string command;
  LinkedList *first;
  LinkedList *last;
  LinkedList x;
  int index = 0;
  LinkedList *temp;
  first = &x;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> key;
      LinkedList *insert_temp = new LinkedList();
      insert_temp->key = key;
      if (index != 0 || first->key != -1) {
        first->is_first = false;
        first->front = insert_temp;

        insert_temp->back = first;
        first = insert_temp;
      } else {
        first = insert_temp;
        last = insert_temp;
      }
      index++;
    } else if (command == "delete") {
      cin >> key;
      temp = first;
      while (true) {
        if (temp->key == key) {
          if (temp->back == NULL) {
            last = temp->front;
            last->back = NULL;
          } else if (temp->front == NULL) {
            first = temp->back;
            first->front = NULL;
          } else {
            temp->front->back = temp->back;
            temp->back->front = temp->front;
          }
          break;
        }
        if (temp->back == NULL)
          break;
        else
          temp = temp->back;
      }

    } else if (first != NULL) {
      if (command == "deleteFirst") {
        first = first->back;
        first->front = NULL;
      } else if (command == "deleteLast") {
        last = last->front;
        last->back = NULL;
      }
    }
  }

  while (true) {
    cout << first->key;
    if (first->back == NULL) {
      break;
    } else {
      cout << " ";
    }
    first = first->back;
  }
  cout << endl;

  return 0;
}