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

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

#define LEN 1000000

using namespace std;

typedef struct Node {
  unsigned int key;
  struct Node *nPrev;
  struct Node *nNext;
} Node;

Node dummy;
unsigned int sum = 0;
void insert(unsigned int n);
void deleteNode(unsigned int n);
void deleteFisrt();
void deleteLast();
void display();

int main() {
  string command;
  int n;
  unsigned input;
  dummy.nPrev = NULL;
  dummy.nNext = NULL;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      scanf("%u", &input);
      insert(input);
    } else if (command == "delete") {
      scanf("%u", &input);
      deleteNode(input);
    } else if (command == "deleteFirst") {
      deleteFisrt();
    } else if (command == "deleteLast") {
      deleteLast();
    } else {
      return -1;
    }
  }
  display();
  return 0;
}

// 先頭に挿入する
void insert(unsigned int n) {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    Node *node = new Node;
    node->key = n;
    node->nPrev = &dummy;
    node->nNext = &dummy;
    dummy.nPrev = node;
    dummy.nNext = node;
    sum++;
  } else {
    Node *node = new Node;
    node->key = n;
    node->nPrev = &dummy;
    node->nNext = dummy.nNext;
    node->nNext->nPrev = node;
    dummy.nNext = node;
    sum++;
  }
}

// 任意のキー値を持つノードを削除する
void deleteNode(unsigned int n) {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  // 確認用に宣言
  Node *pNode;
  pNode = &dummy;

  if (sum == 1) {
    if (dummy.nNext->key == n) {
      delete (dummy.nNext);
      dummy.nNext = NULL;
      dummy.nPrev = NULL;
      sum--;
    }
  } else {
    for (int i = 0; i < sum; i++) {
      if (pNode->nNext->key == n) {
        // Nodeの削除
        pNode->nNext = pNode->nNext->nNext;
        delete (pNode->nNext
                    ->nPrev); // 前の部分は残っているからそこを使ってdelete()
        pNode->nNext->nPrev = pNode;
        sum--;
        break;
      } else {
        // pNodeのnextを次のNodeにする
        pNode = pNode->nNext;
      }
    }
  }
}

// 先頭を削除する
void deleteFisrt() {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  Node *ptmpN = dummy.nNext->nNext;
  delete (dummy.nNext); // 先頭の削除

  // ノードが1つの時と2つ以上の時の条件分岐
  if (&dummy == ptmpN) {
    dummy.nPrev = NULL;
    dummy.nNext = NULL;
  } else {
    dummy.nNext = ptmpN;
    ptmpN->nPrev = &dummy;
  }
}

// 末尾を削除する
void deleteLast() {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  Node *ptmpN = dummy.nPrev->nPrev;
  delete (dummy.nPrev);

  // ノードが1つ以上の時と2つ以上の時の条件分岐
  if (&dummy == ptmpN) {
    dummy.nPrev = NULL;
    dummy.nNext = NULL;
  } else {
    dummy.nPrev = ptmpN;
    ptmpN->nNext = &dummy;
  }
}

// 出力
void display() {

  Node *node = dummy.nNext;
  while (&dummy != node) {
    printf("%d", node->key);
    node = node->nNext;
    delete (node->nPrev);
    if (node != &dummy) {
      printf(" ");
    }
  }
  printf("\\n");
}


Error: WA

Fixed submission:

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

#define LEN 1000000

using namespace std;

typedef struct Node {
  unsigned int key;
  struct Node *nPrev;
  struct Node *nNext;
} Node;

Node dummy;
unsigned int sum = 0;
void insert(unsigned int n);
void deleteNode(unsigned int n);
void deleteFisrt();
void deleteLast();
void display();

int main() {
  string command;
  int n;
  unsigned input;
  dummy.nPrev = NULL;
  dummy.nNext = NULL;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      scanf("%u", &input);
      insert(input);
    } else if (command == "delete") {
      scanf("%u", &input);
      deleteNode(input);
    } else if (command == "deleteFirsrt") {
      deleteFisrt();
    } else if (command == "deleteLast") {
      deleteLast();
    } else {
      return -1;
    }
  }
  display();
  return 0;
}

// 先頭に挿入する
void insert(unsigned int n) {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    Node *node = new Node;
    node->key = n;
    node->nPrev = &dummy;
    node->nNext = &dummy;
    dummy.nPrev = node;
    dummy.nNext = node;
    sum++;
  } else {
    Node *node = new Node;
    node->key = n;
    node->nPrev = &dummy;
    node->nNext = dummy.nNext;
    node->nNext->nPrev = node;
    dummy.nNext = node;
    sum++;
  }
}

// 任意のキー値を持つノードを削除する
void deleteNode(unsigned int n) {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  // 確認用に宣言
  Node *pNode;
  pNode = &dummy;

  if (sum == 1) {
    if (dummy.nNext->key == n) {
      delete (dummy.nNext);
      dummy.nNext = NULL;
      dummy.nPrev = NULL;
      sum--;
    }
  } else {
    for (int i = 0; i < sum; i++) {
      if (pNode->nNext->key == n) {
        // Nodeの削除
        pNode->nNext = pNode->nNext->nNext;
        delete (pNode->nNext
                    ->nPrev); // 前の部分は残っているからそこを使ってdelete()
        pNode->nNext->nPrev = pNode;
        sum--;
        break;
      } else {
        // pNodeのnextを次のNodeにする
        pNode = pNode->nNext;
      }
    }
  }
}

// 先頭を削除する
void deleteFisrt() {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  Node *ptmpN = dummy.nNext->nNext;
  delete (dummy.nNext); // 先頭の削除

  // ノードが1つの時と2つ以上の時の条件分岐
  if (&dummy == ptmpN) {
    dummy.nPrev = NULL;
    dummy.nNext = NULL;
  } else {
    dummy.nNext = ptmpN;
    ptmpN->nPrev = &dummy;
  }
}

// 末尾を削除する
void deleteLast() {
  if (NULL == dummy.nPrev || NULL == dummy.nNext) {
    return;
  }
  Node *ptmpN = dummy.nPrev->nPrev;
  delete (dummy.nPrev);

  // ノードが1つ以上の時と2つ以上の時の条件分岐
  if (&dummy == ptmpN) {
    dummy.nPrev = NULL;
    dummy.nNext = NULL;
  } else {
    dummy.nPrev = ptmpN;
    ptmpN->nNext = &dummy;
  }
}

// 出力
void display() {

  Node *node = dummy.nNext;
  while (&dummy != node) {
    printf("%d ", node->key);
    node = node->nNext;
    delete (node->nPrev);
  }
  printf("\\n");
}