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

#include <stdio.h>
#include <string.h>

const int DEF = -1000000000;
class Cell {
private:
  int val;
  Cell *left, *right;

public:
  Cell() {
    val = DEF;
    left = NULL;
    right = NULL;
  }
  void Left(Cell *l) { left = l; }
  void Right(Cell *r) { right = r; }
  void Update(int x) { val = x; }
  int Value() { return val; }
  Cell *Next() { return right; }
  Cell *Before() { return left; }
};

class DLL {
private:
  Cell *first, *last;

public:
  DLL() {
    first = new Cell();
    last = new Cell();
    first->Right(last);
    last->Left(first);
  }
  void Insert(int x) {
    Cell *nc = new Cell();
    first->Update(x);
    first->Left(nc);
    nc->Right(first);
    first = nc;
  }
  void DeleteFirst() {
    first = first->Next();
    first->Update(DEF);
    delete first->Before();
  }
  void DeleteLast() {
    last = last->Before();
    last->Update(DEF);
    delete last->Next();
  }
  void Delete(int x) {
    Cell *itr = first->Next();
    while (itr != last) {
      if (itr->Value() == x) {
        Cell *f = itr->Before();
        Cell *l = itr->Next();
        f->Right(l);
        l->Left(f);
        delete itr;
        return;
      }
      itr = itr->Next();
    }
  }
  void List() {
    Cell *itr = first->Next();
    printf("%d", itr->Value());
    itr = itr->Next();
    while (itr != last) {
      printf(" %d", itr->Value());
      itr = itr->Next();
    }
    printf("\\n");
  }
};

int main() {
  int n;
  DLL dll;

  scanf("%d", &n);
  char cmd[64];
  int v;
  while (scanf("%s", cmd) != EOF) {
    if (strcmp(cmd, "insert") == 0) {
      scanf("%d", &v);
      dll.Insert(v);
    } else if (strcmp(cmd, "delete") == 0) {
      scanf("%d", &v);
      dll.Delete(v);
    } else if (strcmp(cmd, "deleteFirst") == 0) {
      dll.DeleteFirst();
    } else if (strcmp(cmd, "deleteLast") == 0) {
      dll.DeleteLast();
    } else if (strcmp(cmd, "list") == 0) {
      dll.List();
    }
  }
  dll.List();
  return 0;
}


Error: 0

Fixed submission:

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

typedef struct Unit {
  int v = -1;
  Unit *f = NULL, *l = NULL;
} Unit;

typedef struct List {
  Unit *first, *last;
  Unit u[3000000];

  List() { first = last = u + 1; }
  void ins(long long);
  void delf();
  void dell();
  void del(long long);
  void show();
} List;

void List::ins(long long x) {
  first->f = first + 1;
  first++;
  first->v = x;
  first->l = first - 1;
}
void List::delf() { first = first->l; }
void List::dell() {
  last = last->f;
  last->f->l = last;
}
void List::del(long long x) {
  if (first->v == x) {
    delf();
    return;
  }
  Unit *ite = first;
  while (ite != last) {
    if (ite->v == x) {
      ite->l->f = ite->f;
      ite->f->l = ite->l;
      break;
    }
    ite = ite->l;
  }
}
void List::show() {
  cout << first->v;
  Unit *ite = first->l;
  while (ite != last) {
    cout << " " << ite->v;
    ite = ite->l;
  }
  cout << endl;
}

int n;
long long key;
string cmd;
List li;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> key;
      li.ins(key);
    } else if (cmd == "delete") {
      cin >> key;
      li.del(key);
    } else if (cmd == "deleteFirst") {
      li.delf();
    } else if (cmd == "deleteLast") {
      li.dell();
    }
  }
  li.show();

  return 0;
}