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

#include <iostream>
#include <string>

class list {
private:
  struct TElement {
    int Value;
    TElement *PrevAd;
    TElement *NextAd;
  };
  TElement *StartElement;
  TElement *LastElement;

public:
  class iterator {
    friend class list;

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

  private:
    TElement *Element;
  };

  list();
  ~list();

  void push_front(int A);
  void pop_front();
  void pop_back();
  void erase(iterator itr);
  iterator begin();
  iterator end();
};

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

list::list() {
  TElement *DummyPoint = new TElement;
  *DummyPoint = {0, NULL, NULL};
  StartElement = DummyPoint;
  LastElement = DummyPoint;
}

list::~list() {
  while (StartElement->PrevAd != NULL || StartElement->NextAd != NULL) {
    TElement *NextPoint = StartElement->NextAd;
    delete StartElement;
    StartElement = NextPoint;
    StartElement->PrevAd = NULL;
  }
  delete StartElement;
}

void list::push_front(int A) {
  TElement *AddPoint = new TElement;
  *AddPoint = {A, NULL, StartElement};
  if (StartElement->PrevAd == NULL && StartElement->NextAd == NULL) {
    StartElement = AddPoint;
    LastElement = StartElement->NextAd;
    LastElement->PrevAd = StartElement;
  } else {
    StartElement->PrevAd = AddPoint;
    StartElement = AddPoint;
  }
}

void list::pop_front() {
  if (StartElement->PrevAd != NULL || StartElement->NextAd != NULL) {
    TElement *DeleteNextPoint = StartElement->NextAd;
    (StartElement->NextAd)->PrevAd = NULL;
    delete StartElement;
    StartElement = DeleteNextPoint;
  }
}

void list::pop_back() {
  if (LastElement->PrevAd != NULL || LastElement->NextAd != NULL) {
    TElement *DeletePoint = LastElement->PrevAd;
    LastElement->PrevAd = (LastElement->PrevAd)->PrevAd;
    if (LastElement->PrevAd != NULL) {
      (LastElement->PrevAd)->NextAd = LastElement;
    } else {
      StartElement = LastElement;
    }
    delete DeletePoint;
  }
}

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

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

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

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

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

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

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

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 <iostream>
#include <string>

class list {
private:
  struct TElement {
    int Value;
    TElement *PrevAd;
    TElement *NextAd;
  };
  TElement *StartElement;
  TElement *LastElement;

public:
  class iterator {
    friend class list;

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

  private:
    TElement *Element;
  };

  list();
  ~list();

  void push_front(int A);
  void push_back(int A);
  void pop_front();
  void pop_back();
  void erase(iterator itr);
  iterator begin();
  iterator end();
};

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

list::list() {
  TElement *DummyPoint = new TElement;
  *DummyPoint = {0, NULL, NULL};
  StartElement = DummyPoint;
  LastElement = DummyPoint;
}

list::~list() {
  while (StartElement->PrevAd != NULL || StartElement->NextAd != NULL) {
    TElement *NextPoint = StartElement->NextAd;
    delete StartElement;
    StartElement = NextPoint;
    StartElement->PrevAd = NULL;
  }
  delete StartElement;
}

void list::push_front(int A) {
  TElement *AddPoint = new TElement;
  *AddPoint = {A, NULL, StartElement};
  if (StartElement->PrevAd == NULL && StartElement->NextAd == NULL) {
    StartElement = AddPoint;
    LastElement = StartElement->NextAd;
    LastElement->PrevAd = StartElement;
  } else {
    StartElement->PrevAd = AddPoint;
    StartElement = AddPoint;
  }
}

void list::push_back(int A) {
  TElement *AddPoint = new TElement;
  *AddPoint = {A, LastElement, NULL};
  if (LastElement->PrevAd == NULL && LastElement->NextAd == NULL) {
    StartElement = AddPoint;
  }
  if (LastElement->PrevAd != NULL || LastElement->NextAd != NULL) {
    LastElement->NextAd = AddPoint;
  }
  LastElement = AddPoint;
}

void list::pop_front() {
  if (StartElement->PrevAd != NULL || StartElement->NextAd != NULL) {
    TElement *DeleteNextPoint = StartElement->NextAd;
    (StartElement->NextAd)->PrevAd = NULL;
    delete StartElement;
    StartElement = DeleteNextPoint;
  }
}

void list::pop_back() {
  if (LastElement->PrevAd != NULL || LastElement->NextAd != NULL) {
    TElement *DeletePoint = LastElement->PrevAd;
    LastElement->PrevAd = (LastElement->PrevAd)->PrevAd;
    (LastElement->PrevAd)->NextAd = LastElement;
    delete DeletePoint;
  }
}

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

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

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

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

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

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

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

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;
}