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

#include <cassert>
#include <iostream>
#include <string>

class list {
private:
  struct TElement {
    int Value;
    TElement *PrevAd;
    TElement *NextAd;
  };
  TElement *FStartElement;
  TElement *FLastElement;

public:
  class iterator {
    friend class list;

  public:
    iterator(TElement *AElement);
    bool operator!=(const iterator &itr) const;
    void operator++(int);
    void operator--(int);
    int &operator*();

  private:
    TElement *FElement;
  };
  list();
  ~list();
  void push_front(int Value);
  void pop_front();
  void pop_back();
  void erase(iterator itr);
  iterator begin();
  iterator end();
};

list::list() {
  TElement *DummyPoint = new TElement;
  *DummyPoint = {0, NULL, NULL};
  FStartElement = DummyPoint;
  FLastElement = DummyPoint;
}

list::~list() {
  while (FStartElement) {
    TElement *NextPoint = FStartElement->NextAd;
    delete FStartElement;
    FStartElement = NextPoint;
  }
}

void list::push_front(int Value) {
  TElement *AddPoint = new TElement;
  *AddPoint = {Value, NULL, FStartElement};
  if (FStartElement == FLastElement) {
    FStartElement = AddPoint;
    FLastElement = FStartElement->NextAd;
    FLastElement->PrevAd = FStartElement;
  } else {
    FStartElement->PrevAd = AddPoint;
    FStartElement = AddPoint;
  }
}

void list::pop_front() {
  assert(FStartElement != FLastElement);
  TElement *DeleteNextPoint = FStartElement->NextAd;
  DeleteNextPoint->PrevAd = NULL;
  delete FStartElement;
  FStartElement = DeleteNextPoint;
}

void list::pop_back() {
  assert(FStartElement != FLastElement);
  TElement *DeletePoint = FLastElement->PrevAd;
  FLastElement->PrevAd = DeletePoint->PrevAd;
  if (FLastElement->PrevAd != NULL) {
    (FLastElement->PrevAd)->NextAd = FLastElement;
  } else {
    FStartElement = FLastElement;
  }
  delete DeletePoint;
}

void list::erase(iterator itr) {
  if (itr.FElement->PrevAd != NULL) {
    (itr.FElement->PrevAd)->NextAd = itr.FElement->NextAd;
    (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd;
  } else {
    (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd;
    FStartElement = itr.FElement->NextAd;
  }
  delete itr.FElement;
}

list::iterator::iterator(TElement *AElement) { FElement = AElement; }

list::iterator list::begin() {
  iterator itr = {FStartElement};
  return itr;
}

list::iterator list::end() {
  iterator itr = {FLastElement};
  return itr;
}

bool list::iterator::operator!=(const iterator &itr) const {
  return itr.FElement != FElement;
}

void list::iterator::operator++(int) { FElement = FElement->NextAd; }

void list::iterator::operator--(int) { FElement = FElement->PrevAd; }

int &list::iterator::operator*() { return FElement->Value; }

void Command(list &X, std::string &c) {
  if (c == "insert") {
    int d;
    std::cin >> d;
    X.push_front(d);
  } else if (c == "delete") {
    int d;
    std::cin >> d;
    list::iterator itr = X.begin();
    while (*itr != d && itr != X.end()) {
      itr++;
    }
    if (itr != X.end()) {
      X.erase(itr);
    }
  } else if (c == "deleteFirst") {
    X.pop_front();
  } else if (c == "deleteLast") {
    X.pop_back();
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  list X;
  for (int i = 0; i < n; i++) {
    std::string c;
    std::cin >> c;
    Command(X, c);
  }
  list::iterator it = X.begin();
  list::iterator end = X.end();
  end--;
  while (it != end) {
    std::cout << *it << ' ';
    it++;
  }
  std::cout << *end << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <string>

class list {
private:
  struct TElement {
    int Value;
    TElement *PrevAd;
    TElement *NextAd;
  };
  TElement *FStartElement;
  TElement *FLastElement;

public:
  class iterator {
    friend class list;

  public:
    iterator(TElement *AElement);
    bool operator!=(const iterator &itr) const;
    void operator++(int);
    void operator--(int);
    int &operator*();

  private:
    TElement *FElement;
  };
  list();
  ~list();
  void push_front(int Value);
  void pop_front();
  void pop_back();
  void erase(iterator itr);
  iterator begin();
  iterator end();
};

list::list() {
  TElement *DummyPoint = new TElement;
  *DummyPoint = {0, NULL, NULL};
  FStartElement = DummyPoint;
  FLastElement = DummyPoint;
}

list::~list() {
  while (FStartElement) {
    TElement *NextPoint = FStartElement->NextAd;
    delete FStartElement;
    FStartElement = NextPoint;
  }
}

void list::push_front(int Value) {
  TElement *AddPoint = new TElement;
  *AddPoint = {Value, NULL, FStartElement};
  if (FStartElement == FLastElement) {
    FStartElement = AddPoint;
    FLastElement = FStartElement->NextAd;
    FLastElement->PrevAd = FStartElement;
  } else {
    FStartElement->PrevAd = AddPoint;
    FStartElement = AddPoint;
  }
}

void list::pop_front() {
  assert(FStartElement != FLastElement);
  TElement *DeleteNextPoint = FStartElement->NextAd;
  DeleteNextPoint->PrevAd = NULL;
  delete FStartElement;
  FStartElement = DeleteNextPoint;
}

void list::pop_back() {
  assert(FStartElement != FLastElement);
  TElement *DeletePoint = FLastElement->PrevAd;
  FLastElement->PrevAd = DeletePoint->PrevAd;
  DeletePoint->PrevAd->NextAd = FLastElement;
  delete DeletePoint;
}

void list::erase(iterator itr) {
  if (itr.FElement->PrevAd != NULL) {
    (itr.FElement->PrevAd)->NextAd = itr.FElement->NextAd;
    (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd;
  } else {
    (itr.FElement->NextAd)->PrevAd = itr.FElement->PrevAd;
    FStartElement = itr.FElement->NextAd;
  }
  delete itr.FElement;
}

list::iterator::iterator(TElement *AElement) { FElement = AElement; }

list::iterator list::begin() {
  iterator itr = {FStartElement};
  return itr;
}

list::iterator list::end() {
  iterator itr = {FLastElement};
  return itr;
}

bool list::iterator::operator!=(const iterator &itr) const {
  return itr.FElement != FElement;
}

void list::iterator::operator++(int) { FElement = FElement->NextAd; }

void list::iterator::operator--(int) { FElement = FElement->PrevAd; }

int &list::iterator::operator*() { return FElement->Value; }

void Command(list &X, std::string &c) {
  if (c == "insert") {
    int d;
    std::cin >> d;
    X.push_front(d);
  } else if (c == "delete") {
    int d;
    std::cin >> d;
    list::iterator itr = X.begin();
    while (*itr != d && itr != X.end()) {
      itr++;
    }
    if (itr != X.end()) {
      X.erase(itr);
    }
  } else if (c == "deleteFirst") {
    X.pop_front();
  } else if (c == "deleteLast") {
    X.pop_back();
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  list X;
  for (int i = 0; i < n; i++) {
    std::string c;
    std::cin >> c;
    Command(X, c);
  }
  list::iterator it = X.begin();
  list::iterator end = X.end();
  end--;
  while (it != end) {
    std::cout << *it << ' ';
    it++;
  }
  std::cout << *end << std::endl;
  return 0;
}