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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

struct Node {
  int key;
  Node *prev;
  Node *next;
};

Node *head;

Node *listSearch(int key) {
  Node *cur = head->next;
  while (cur != head && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

void init() {
  head = new Node;
  head->prev = head;
  head->next = head;
}

void printlist() {
  Node *cur = head->next;
  int isf = 0;
  while (1) {
    if (cur == head)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

void insert(int key) {
  Node *x = new Node;
  x->key = key;
  x->next = head->next;
  head->next->prev = x;
  head->next = x;
  x->prev = head;
}

void deleteNode(Node *t) {
  if (t == head)
    return;
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

void deleteFirst() { deleteNode(head->next); }

void deleteLast() { deleteNode(head->prev); }

void deletekey(int key) { deleteNode(listSearch(key)); }

int main() {
  int key, n, i;
  char com[20];
  scanf("%d", &n);
  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i') {
      insert(key);
    } else if (com[0] == 'd') {
      if (strlen(com) > 6) {
        if (com[6] == 'F')
          deleteFirst();
        else if (com[6] == 'L')
          deleteLast();
      } else {
        deletekey(key);
      }
    }
  }
  printlist();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct Node {
  int v;
  Node *prev;
  Node *next;
};

Node *head;
Node *tail;

void init() {
  head = new Node;
  tail = new Node;
  head->prev = NULL;
  head->next = tail;
  tail->prev = head;
  tail->next = NULL;
}

void insert(Node *p, int value) {
  if (!p)
    return;
  Node *nextP = p->next;
  Node *newP = new Node;
  newP->v = value;

  p->next = newP;
  newP->prev = p;
  newP->next = nextP;
  nextP->prev = newP;
}

Node *listSearch(int value) {
  Node *cur = head->next;
  while (cur != NULL && cur != tail && cur->v != value) {
    cur = cur->next;
  }
  return cur;
}

void insert(int value) { insert(head, value); }

void del(Node *p) {
  if (!p)
    return;
  Node *pre = p->prev;
  Node *nex = p->next;
  if (pre) {
    pre->next = nex;
  }
  if (nex) {
    nex->prev = pre;
  }
  delete p;
}

void delFirst() {
  if (head->next && head->next != tail) {
    del(head->next);
  }
}

void delLast() {
  if (tail->prev && tail->prev != head) {
    del(tail->prev);
  }
}

char cmd[10];
int v;
int n;

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%d", &n);
  init();
  while (n--) {
    scanf("%s", cmd);
    if (cmd[0] == 'i') {
      scanf("%d", &v);
      insert(v);
    } else {
      if (strlen(cmd) > 6) {
        if (cmd[6] == 'F') {
          delFirst();
        } else {
          delLast();
        }
      } else {
        scanf("%d", &v);
        Node *p = listSearch(v);
        del(p);
      }
    }
  }
  for (Node *p = head->next; p != tail; p = p->next) {
    if (p != head->next)
      printf(" ");
    printf("%d", p->v);
  }
  printf("\\n");
  return 0;
}