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

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

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

int main() {

  int order_num;
  char order[10];
  int val;
  node *p = (node *)malloc(sizeof(node));
  node *origin = p;
  origin->next = NULL;
  origin->prev = NULL;
  origin->num = 0;
  cin >> order_num;
  const int test = order_num;

  for (int i = 0; i < test; i++) {
    cin >> order;
    // cout << i << "/" << test << ":" << order << endl;
    if (order[0] == 'i') {
      cin >> val;
      p->num = val;
      p->next = (node *)malloc(sizeof(node));
      p->next->prev = p;
      p->next->next = NULL;
      p->next->num = 0;
      p = p->next;
    } else if (order[6] == 'F') {
      if (p->prev == origin) {
        free(origin);
        origin = p;
        p->prev = NULL;
      } else {
        p->prev = p->prev->prev;
        free(p->prev->next);
        p->prev->next = p;
      }
    } else if (order[6] == 'L') {
      origin = origin->next;
      free(origin->prev);
      origin->prev = NULL;
    } else {
      cin >> val;
      node *p2 = p;
      do {
        p2 = p2->prev;
        if (p2->num == val) {
          if (p2->prev == NULL) {
            origin = origin->next;
            free(origin->prev);
            origin->prev = NULL;
          } else {
            p2->next->prev = p2->prev;
            p2->prev->next = p2->next;
            free(p2);
          }
          break;
        }
      } while (p2->prev != NULL);
    }
  }

  if (p != origin) {
    p = p->prev;
    while (p->prev != NULL) {
      free(p->next);
      cout << p->num << " ";
      p = p->prev;
    }
    cout << p->num << endl;
  }
  free(p);

  return 0;
}

Error: 0

Fixed submission:

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

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

int main() {

  int order_num;
  char order[10];
  int val;
  node *p = (node *)malloc(sizeof(node));
  node *origin = p;
  origin->next = NULL;
  origin->prev = NULL;
  origin->num = 0;
  cin >> order_num;
  const int test = order_num;

  for (int i = 0; i < test; i++) {
    cin >> order;
    // cout << i << "/" << test << ":" << order << endl;
    if (order[0] == 'i') {
      cin >> val;
      p->num = val;
      p->next = (node *)malloc(sizeof(node));
      p->next->prev = p;
      p->next->next = NULL;
      p = p->next;
    } else if (order[6] == 'F') {
      p->prev = p->prev->prev;
      free(p->prev->next);
      p->prev->next = p;
    } else if (order[6] == 'L') {
      origin = origin->next;
      free(origin->prev);
      origin->prev = NULL;
    } else {
      cin >> val;
      node *p2 = p;
      do {
        p2 = p2->prev;
        if (p2->num == val) {
          if (p2->prev == NULL) {
            origin = origin->next;
            free(origin->prev);
            origin->prev = NULL;
          } else {
            p2->next->prev = p2->prev;
            p2->prev->next = p2->next;
            free(p2);
          }
          break;
        }
      } while (p2->prev != NULL);
    }

    node *buf = p->prev;
    /*
    cout <<"	";
    while(buf->prev != NULL){
            cout << buf->num << " ";
            buf = buf->prev;
    }
    cout << buf->num << endl;
    */
  }

  p = p->prev;
  while (p->prev != NULL) {
    free(p->next);
    cout << p->num << " ";
    p = p->prev;
  }
  cout << p->num << endl;
  free(p);

  return 0;
}