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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

struct node {
  node *next;
  node *prev;
  int num;
};

node *head;

void initialize() {
  head = (node *)malloc(sizeof(node));
  head->next = head;
  head->prev = head;
}

void insert(int m) {
  node *p = (node *)malloc(sizeof(node));

  p->num = m;
  p->next = head->next;
  head->next->prev = p;
  p->prev = head;
  head->next = p;
}

void del(node *p) {
  p->prev->next = p->next;
  p->next->prev = p->prev;

  free(p);
}

void deletefirst() {
  del(head->next);
  /*head->next->next->prev = head;
  free(head->next);
  head->next = head->next->next;*/
}

void deletelast() {
  del(head->prev);
  /*head->prev->prev->next= head;
  free(head->prev);
  head->prev = head->prev->prev;*/
}

node *search(int key, int count) {
  node *cur = (node *)malloc(sizeof(node));
  cur = head->next;

  for (int i = 0; i < count; i++) {
    if (cur->num == key) {
      return cur;
    }
    cur = cur->next;
  }

  return NULL;
}

int main() {
  node *pp = (node *)malloc(sizeof(node));
  int n;
  int key;
  char str[12];

  int count = 0;

  initialize();

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s", str);

    if (str[6] == 'F') {
      deletefirst();
      count--;

    } else if (str[6] == 'L') {
      deletelast();
      count--;

    } else {
      scanf("%d", &key);

      if (str[0] == 'i') {
        insert(key);
        count++;

      } else if (str[0] == 'd') {
        pp = search(key, count);
        if (pp != NULL) {
          del(pp);
          count--;
        }
      }
    }
  }

  pp = head->next;

  for (int i = 1; i <= count; i++) {
    if (i == count) {
      printf("%d\\n", pp->num);
    } else {
      printf("%d ", pp->num);
    }
    pp = pp->next;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

struct node {
  node *next;
  node *prev;
  int num;
};

/*node* nodemalloc()
{
        return (node *)malloc( sizeof(node) );
}*/

node *head;

void initialize() {
  head = (node *)malloc(sizeof(node));
  head->next = head;
  head->prev = head;
}

void insert(int m) {
  node *p = (node *)malloc(sizeof(node));

  p->num = m;
  p->next = head->next;
  head->next->prev = p;
  p->prev = head;
  head->next = p;

  // cout << "p-> = " << p->num << "   " << "p->prev->num = " << p->prev->num <<
  // "\\n"; cout << "p->next->prev->num = " << p->next->prev->num << "\\n";
}

void del(node *p) {
  p->prev->next = p->next;
  p->next->prev = p->prev;

  /*cout << "p->num = " << p->num << "\\n";
  cout << "p->prev =" << p->prev->num << "\\n";
  cout << "p->prev->next =" << p->prev->next->num << "\\n";
  */
  free(p);
}

void deletefirst() {
  head->next->next->prev = head;
  free(head->next);
  head->next = head->next->next;
}

void deletelast() {
  head->prev->prev->next = head;
  free(head->prev);
  head->prev = head->prev->prev;
}

node *search(int key) {
  node *cur = (node *)malloc(sizeof(node));
  cur = head->next;

  while (cur != head && cur->num != key) {
    cur = cur->next;
  }
  return cur;
}

int main() {
  node *pp = (node *)malloc(sizeof(node));
  int n;
  int key;
  char str[15];

  // pp = nodemalloc();

  initialize();

  scanf("%d", &n);

  cout << "\\n n=" << n << "\\n";

  for (int i = 0; i < n; i++) {
    scanf("%s", str);

    if (str[6] == 'F') {
      deletefirst();

    } else if (str[6] == 'L') {
      deletelast();

    } else {
      scanf("%d", &key);

      if (str[0] == 'i') {
        insert(key);

      } else if (str[0] == 'd') {
        pp = search(key);
        del(pp);
      }
    }
  }

  // cout << "ok3\\n";

  pp = head->next;

  while (1) {
    if (pp->next != head) {
      cout << pp->num << " ";
    } else if (pp->next == head) {
      cout << pp->num << "\\n";
      break;
    }
    pp = pp->next;
  }

  // cout << "ok4\\n";

  return 0;
}