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

#include <iostream>
#include <memory>

using namespace std;

struct node {
  int key;
  node *next;
  node *prev;
  //  ~node(){cout << "[delete" << key << "]" << endl;}
};

void print_nodes(node *p) {
  if (!p) {
    cout << endl;
    return;
  }
  cout << p->key;
  node *q;
  if (q = p->next)
    while (auto r = q) {
      cout << " " << r->key;
      q = r->next;
    }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  node *np;
  node *npl;
  np = nullptr;
  npl = nullptr;
  int n, key;
  string str;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> key;
      node *p = new node();
      p->key = key;
      p->prev = nullptr;
      if (np == nullptr) {
        p->next = nullptr;
        np = p;
        npl = p;
      } else {
        p->next = np;
        np->prev = p;
        np = p;
      }
    } else if (str == "delete") {
      cin >> key;
      node *search = np;
      for (;;) {
        if (!search)
          break;
        if (search->key == key) {
          if (np == npl) {
            delete np;
            np = nullptr;
            npl = nullptr;
          } else {
            if (search != npl)
              search->next->prev = search->prev;
            else
              npl = npl->prev;
            if (search != np)
              search->prev->next = search->next;
            else
              np = np->next;
            delete search;
          }
          break;
        }
        search = search->next;
      }
    } else if (str == "deleteFirst") {
      if (np != npl) {
        np = np->next;
        delete np->prev;
        np->prev = nullptr;
      } else {
        delete np;
        np = nullptr;
        npl = nullptr;
      }
    } else if (str == "deleteLast") {
      if (np != npl) {
        npl = npl->prev;
        delete npl->next;
        npl->next = nullptr;
      } else {
        delete np;
        np = nullptr;
        npl = nullptr;
      }
    }
  }
  print_nodes(np);
  node *p = np;
  while (node *q = p) {
    p = p->next;
    delete q;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <memory>

using namespace std;

struct node {
  int key;
  node *next;
  node *prev;
  //  ~node(){cout << "[delete" << key << "]" << endl;}
};

void print_nodes(node *p) {
  if (!p) {
    cout << endl;
    return;
  }
  cout << p->key;
  node *q;
  if (q = p->next)
    while (auto r = q) {
      cout << " " << r->key;
      q = r->next;
    }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  node *np;
  node *npl;
  np = nullptr;
  npl = nullptr;
  int n, key;
  string str;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> key;
      node *p = new node();
      p->key = key;
      p->prev = nullptr;
      if (np == nullptr) {
        p->next = nullptr;
        np = p;
        npl = p;
      } else {
        p->next = np;
        np->prev = p;
        np = p;
      }
    } else if (str == "delete") {
      cin >> key;
      node *search = np;
      for (;;) {
        if (!search)
          break;
        if (search->key == key) {
          if (search->next)
            search->next->prev = search->prev;
          else
            npl = search->prev;
          if (search->prev)
            search->prev->next = search->next;
          else
            np = search->next;
          delete search;
          break;
        }
        search = search->next;
      }
    } else if (str == "deleteFirst") {
      if (np != npl) {
        np = np->next;
        delete np->prev;
        np->prev = nullptr;
      } else {
        delete np;
        np = npl = nullptr;
      }
      delete np->prev;
    } else if (str == "deleteLast") {
      if (np != npl) {
        npl = npl->prev;
        delete npl->next;
        npl->next = nullptr;
      } else {
        delete np;
        np = nullptr;
        npl = nullptr;
      }
    }
  }
  print_nodes(np);
  node *p = np;
  while (auto q = p) {
    delete q;
    p = p->next;
  }
  return 0;
}