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

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define BUF_SIZE 1024

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

class ConnectedList {
public:
  ConnectedList() {
    motherNode = new Node();
    motherNode->data = -1;
    motherNode->prev = motherNode;
    motherNode->next = motherNode;
  };
  ~ConnectedList() {
    Node *tmpNode = motherNode->next;
    Node *nextNode;
    while (tmpNode != motherNode) {
      nextNode = tmpNode->next;
      delete tmpNode;
      tmpNode = nextNode;
    }
    delete motherNode;
  };
  void insertNode(int x) {
    Node *tmpNode = motherNode->next;
    motherNode->next = new Node();
    motherNode->next->data = x;
    motherNode->next->next = tmpNode;
    motherNode->next->prev = motherNode;
    tmpNode->prev = motherNode->next;
  };
  void deleteNode(int x) {
    Node *tmpNode = motherNode->next;
    while (tmpNode != motherNode && tmpNode->data != x) {
      tmpNode = tmpNode->next;
    }
    if (tmpNode != motherNode) {
      tmpNode->prev->next = tmpNode->next;
      tmpNode->next->prev = tmpNode->prev;
      delete tmpNode;
    }
  };

  void deleteFirst() {
    Node *tmpNode = motherNode->next;
    motherNode->next = motherNode->next->next;
    motherNode->next->prev = motherNode;
    delete tmpNode;
  };

  void deleteLast() {
    Node *tmpNode = motherNode->prev;
    motherNode->prev = motherNode->prev->prev;
    motherNode->prev->next = motherNode;
    delete tmpNode;
  };

  void showList() {
    Node *tmpNode = motherNode->next;
    printf("%d", tmpNode->data);
    tmpNode = tmpNode->next;
    while (tmpNode != motherNode) {
      printf(" %d", tmpNode->data);
      tmpNode = tmpNode->next;
    }
    printf("\\n");
  }

private:
  Node *motherNode;
};

int main() {

  char buf[BUF_SIZE];
  int n = atoi(fgets(buf, BUF_SIZE, stdin));

  ConnectedList list;

  for (int i = 0; i < n; i++) {

    fgets(buf, BUF_SIZE, stdin);

    if (buf[6] == 'F') {
      list.deleteFirst();
    } else if (buf[6] == 'L') {
      list.deleteLast();
    } else {
      int S = 0;

      if (buf[0] == 'i') { // insert x

        for (int i = 0; buf[i] != '\\0'; i++) {
          if (buf[i] >= '0' && buf[i] <= '9') {
            S = 10 * S + (buf[i] - '0');
          }
        }
        list.insertNode(S);
      } else {
        for (int i = 0; buf[i] != '\\0'; i++) {
          if (buf[i] >= '0' && buf[i] <= '9') {
            S = 10 * S + (buf[i] - '0');
          }
        }
        list.deleteNode(S);
      }
    }
  }

  list.showList();
}

Error: -11

Fixed submission:

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

typedef struct ListNode {
  int data;
  struct ListNode *next;
  struct ListNode *front;
} Node;
Node *head, *back;

Node *init() {
  Node *p = (Node *)malloc(sizeof(Node));
  p->data = -1;
  p->front = p->next = NULL;
  return p;
}

void insert_(int x) {
  Node *p = init();
  back->next = p;
  p->front = back;
  p->data = x;
  back = p;
}
void delete_(int x) {
  for (Node *p = head->next; p != back->next; p = p->next) {
    if (p->data == x) {
      if (p != back) {
        p->front->next = p->next;
        p->next->front = p->front;
      } else {
        back = back->front;
      }
      free(p);
      break;
    }
  }
}
void deleteFirst() {
  if (back == head->next)
    back = head;
  Node *p = head->next;
  head->next = p->next;
  p->next->front = head;
  free(p);
}
void deleteLast() {
  Node *p = back;
  back = back->front;
  free(p);
}
int main() {
  head = back = init();
  int n, x;
  char *in;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", in);
    if (strlen(in) < 6) {
      if (in[0] == 'i') {
        scanf("%d", &x);
        insert_(x);
      } else {
        scanf("%d", &x);
        delete_(x);
      }
    } else {
      if (in[6] == 'F')
        deleteFirst();
      else
        deleteLast();
    }
  }
  for (Node *p = head->next; p != back->next && p; p = p->next)
    printf("%d ", p->data);
  return 0;
}