prithivMLmods/Coder-Stat huggingface dataset:
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>

using namespace std;

int main() {

  int n;
  cin >> n;

  list<int> L;

  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;

    if (s == "insert") {
      int v;
      scanf("%d", &v);
      L.push_front(v);
    } else if (s == "delete") {
      int v;
      scanf("%d", &v);
      for (auto itr = L.begin(); itr != L.end(); ++itr) {
        if (*itr == v) {
          L.erase(itr);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      L.pop_front();
    } else {
      L.pop_back();
    }
  }

  for (auto itr = L.begin(); itr != L.end(); ++itr) {
    if (itr != L.begin()) {
      cout << " " << *itr;
    } else {
      cout << *itr;
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

  list<int> L;

  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;

    if (s == "insert") {
      int v;
      scanf("%d", &v);
      L.push_front(v);
    } else if (s == "delete") {
      int v;
      scanf("%d", &v);
      for (auto itr = L.begin(); itr != L.end(); ++itr) {
        if (*itr == v) {
          L.erase(itr);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      L.pop_front();
    } else {
      L.pop_back();
    }
  }

  for (auto itr = L.begin(); itr != L.end(); ++itr) {
    if (itr != L.begin()) {
      cout << " " << *itr;
    } else {
      cout << *itr;
    }
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <list>

using namespace std;

int main() {
  list<int> list;
  int n;
  cin >> n;

  string cmd;
  int i, arg;
  for (i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> arg;
      list.push_front(arg);
    } else if (cmd == "delete") {
      cin >> arg;
      int pos = 0;
      for (int tmp : list) {
        if (tmp == arg)
          break;
        pos++;
      }
      list.erase(next(list.begin(), pos));
    } else if (cmd == "deleteFirst") {
      list.pop_front();
    } else if (cmd == "deleteLast") {
      list.pop_back();
    }
  }

  int size = static_cast<int>(list.size());
  if (size > 0) {
    cout << list.front();
    list.pop_front();
    for (i = 1; i < size; i++) {
      cout << ' ' << list.front();
      list.pop_front();
    }
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <list>

using namespace std;

int main() {
  list<int> list;
  int n;
  cin >> n;

  string cmd;
  int i, arg;
  for (i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> arg;
      list.push_front(arg);
    } else if (cmd == "delete") {
      cin >> arg;
      int pos = 0;
      for (int tmp : list) {
        if (tmp == arg)
          break;
        pos++;
      }
      if (pos != static_cast<int>(list.size()))
        list.erase(next(list.begin(), pos));
    } else if (cmd == "deleteFirst") {
      list.pop_front();
    } else if (cmd == "deleteLast") {
      list.pop_back();
    }
  }

  int size = static_cast<int>(list.size());
  if (size > 0) {
    cout << list.front();
    list.pop_front();
    for (i = 1; i < size; i++) {
      cout << ' ' << list.front();
      list.pop_front();
    }
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int main() {
  list<int> s;
  list<int>::iterator it, endit;
  int n;
  cin >> n;
  pair<string, int> p;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str != "deleteFirst" && str != "deleteLast") {
      cin >> p.second;
      p.first = str;
      if (p.first == "insert")
        s.push_front(p.second);
      else if (p.first == "delete") {
        it = s.begin(), endit = s.end();
        for (; it != endit; ++it) {
          if (*it == p.second) {
            s.erase(it);
            break;
          }
        }
      }
    } else if (str == "deleteFirst") {
      s.pop_front();
    } else
      s.pop_back();
  }
  cout << s.front();
  s.pop_front();
  for (; !s.empty();) {
    cout << " " << s.front();
    s.pop_front();
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  list<int> s;
  list<int>::iterator it, endit;
  int n;
  cin >> n;
  pair<string, int> p;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str != "deleteFirst" && str != "deleteLast") {
      cin >> p.second;
      p.first = str;
      if (p.first == "insert")
        s.push_front(p.second);
      else if (p.first == "delete") {
        it = s.begin(), endit = s.end();
        for (; it != endit; ++it) {
          if (*it == p.second) {
            s.erase(it);
            break;
          }
        }
      }
    } else if (str == "deleteFirst") {
      s.pop_front();
    } else
      s.pop_back();
  }
  cout << s.front();
  s.pop_front();
  for (; !s.empty();) {
    cout << " " << s.front();
    s.pop_front();
  }
  cout << endl;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

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

class List {
  Node *nil;
  void delete_node(Node *n) {
    n->prev->next = n->next;
    n->next->prev = n->prev;
    delete n;
  }

public:
  List() {
    nil = new Node;
    nil->prev = nil;
    nil->next = nil;
  }
  void insert_key(int key) {
    Node *n = new Node;
    n->key = key;
    nil->next->prev = n;
    n->next = nil->next;
    nil->next = n;
    n->prev = nil;
  }
  void delete_key(int key) {
    for (Node *n = nil->next; n != nil; n = n->next) {
      if (n->key == key) {
        delete_node(n);
        return;
      }
    }
  }
  void print_all() {
    for (Node *n = nil->next; n != nil; n = n->next) {
      if (n != nil->next)
        cout << " ";
      cout << n->key;
    }
    cout << endl;
  }
  void deleteFirst() { delete_node(nil->next); }
  void deleteLast() { delete_node(nil->prev); }
};

int main() {
  int n;
  cin >> n;
  List l;
  for (int i = 0; i < n; i++) {
    string command;
    cin >> command;
    if (command == "deleteFirst") {
      l.deleteFirst();
      continue;
    }
    if (command == "deleteLast") {
      l.deleteLast();
      continue;
    }
    int key;
    cin >> key;
    if (command == "insert") {
      l.insert_key(key);
      continue;
    }
    if (command == "delete") {
      l.delete_key(key);
      continue;
    }
  }
  l.print_all();
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

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

class List {
  Node *nil;
  void delete_node(Node *n) {
    n->prev->next = n->next;
    n->next->prev = n->prev;
    delete n;
  }

public:
  List() {
    nil = new Node;
    nil->prev = nil;
    nil->next = nil;
  }
  void insert_key(int key) {
    Node *n = new Node;
    n->key = key;
    nil->next->prev = n;
    n->next = nil->next;
    nil->next = n;
    n->prev = nil;
  }
  void delete_key(int key) {
    for (Node *n = nil->next; n != nil; n = n->next) {
      if (n->key == key) {
        delete_node(n);
        return;
      }
    }
  }
  void print_all() {
    for (Node *n = nil->next; n != nil; n = n->next) {
      if (n != nil->next)
        cout << " ";
      cout << n->key;
    }
    cout << endl;
  }
  void deleteFirst() { delete_node(nil->next); }
  void deleteLast() { delete_node(nil->prev); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  List l;
  for (int i = 0; i < n; i++) {
    string command;
    cin >> command;
    if (command == "deleteFirst") {
      l.deleteFirst();
      continue;
    }
    if (command == "deleteLast") {
      l.deleteLast();
      continue;
    }
    int key;
    cin >> key;
    if (command == "insert") {
      l.insert_key(key);
      continue;
    }
    if (command == "delete") {
      l.delete_key(key);
      continue;
    }
  }
  l.print_all();
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
  list<int> L;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "deleteFirst") {
      L.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      L.pop_back();
      continue;
    }
    int num;
    cin >> num;
    if (s == "insert") {
      L.push_front(num);
      continue;
    }
    if (s == "delete") {
      for (list<int>::iterator it = L.begin(); it != L.end(); it++) {
        if (*it == num) {
          L.erase(it);
          break;
        }
      }
    }
  }

  for (list<int>::iterator it = L.begin(); it != L.end();) {
    cout << *it;
    if (++it != L.end()) {
      cout << " ";
    } else {
      cout << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  list<int> L;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "deleteFirst") {
      L.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      L.pop_back();
      continue;
    }
    int num;
    cin >> num;
    if (s == "insert") {
      L.push_front(num);
      continue;
    }
    if (s == "delete") {
      for (list<int>::iterator it = L.begin(); it != L.end(); it++) {
        if (*it == num) {
          L.erase(it);
          break;
        }
      }
    }
  }

  for (list<int>::iterator it = L.begin(); it != L.end();) {
    cout << *it;
    if (++it != L.end()) {
      cout << " ";
    } else {
      cout << endl;
    }
  }

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  int n;
  string st;
  int x;
  cin >> n;
  list<int> L;
  list<int>::iterator it, endit;

  while (n--) {
    cin >> st;

    if (st == "insert") {
      cin >> x;
      L.push_front(x);
    }

    else if (st == "delete") {
      cin >> x;
      for (it = L.begin(), endit = L.end(); it != endit; ++it) {
        if (*it == x) {
          L.erase(it);
          break;
        }
      }
    }

    else if (st == "deleteFirst") {
      L.pop_front();
    }

    else { // deleteLast
      L.pop_back();
    }
  }

  for (it = L.begin(), endit = --L.end(); it != endit; ++it) {
    cout << *it << ' ';
  }
  cout << *it << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  string st;
  int x;
  cin >> n;
  list<int> L;
  list<int>::iterator it, endit;

  while (n--) {
    cin >> st;

    if (st == "insert") {
      cin >> x;
      L.push_front(x);
    }

    else if (st == "delete") {
      cin >> x;
      for (it = L.begin(), endit = L.end(); it != endit; ++it) {
        if (*it == x) {
          L.erase(it);
          break;
        }
      }
    }

    else if (st == "deleteFirst") {
      L.pop_front();
    }

    else { // deleteLast
      L.pop_back();
    }
  }

  for (it = L.begin(), endit = --L.end(); it != endit; ++it) {
    cout << *it << ' ';
  }
  cout << *it << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <stdio.h>
#include <string>

using namespace std;
list<int> lst;

int main() {
  int dataset;
  scanf("%d", &dataset);
  for (int i = 0; i < dataset; i++) {
    string order;
    cin >> order;
    if (order[0] == 'i') {
      int a;
      scanf("%d", &a);
      lst.push_front(a);
    } else if (order[6] == 'F') {
      lst.pop_front();
    } else if (order[6] == 'L') {
      lst.pop_back();
    } else {
      int order;
      cin >> order;
      auto itr = lst.begin();
      int size = lst.size();
      for (int i = 0; i < size; i++) {
        if (*itr == order) {
          lst.erase(itr);
          break;
        }
        itr++;
      }
    }
  }
  for (auto itr = lst.begin(); itr != lst.end(); itr++) {
    if (itr != lst.begin()) {
      printf(" ");
    }
    printf("%d", *itr);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <stdio.h>
#include <string>

using namespace std;
list<int> lst;

int main() {
  int dataset;
  scanf("%d", &dataset);
  for (int i = 0; i < dataset; i++) {
    char order[20];
    scanf("%s", order);
    if (order[0] == 'i') {
      int a;
      scanf("%d", &a);
      lst.push_front(a);
    } else if (order[6] == 'F') {
      lst.pop_front();
    } else if (order[6] == 'L') {
      lst.pop_back();
    } else {
      int order;
      cin >> order;
      auto itr = lst.begin();
      int size = lst.size();
      for (int i = 0; i < size; i++) {
        if (*itr == order) {
          lst.erase(itr);
          break;
        }
        itr++;
      }
    }
  }
  for (auto itr = lst.begin(); itr != lst.end(); itr++) {
    if (itr != lst.begin()) {
      printf(" ");
    }
    printf("%d", *itr);
  }
  printf("\n");
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s;
  int n, i, a;
  list<int> li;
  list<int>::iterator p = li.begin();
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> a;
      li.push_front(a);
    } else if (s == "delete") {
      cin >> a;
      p = find(li.begin(), li.end(), a);
      li.erase(p);

    } else if (s == "deleteFirst") {
      li.pop_front();
    } else if (s == "deleteLast") {
      li.pop_back();
    }
  }
  p = li.begin();

  while (p != li.end()) {
    cout << *p;
    p++;
    if (p != li.end())
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s;
  int n, i, a;
  list<int> li;
  list<int>::iterator p = li.begin();
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> a;
      li.push_front(a);
    } else if (s == "delete") {
      cin >> a;
      p = find(li.begin(), li.end(), a);
      if (p != li.end())
        li.erase(p);

    } else if (s == "deleteFirst") {
      li.pop_front();
    } else if (s == "deleteLast") {
      li.pop_back();
    }
  }
  p = li.begin();

  while (p != li.end()) {
    cout << *p;
    p++;
    if (p != li.end())
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {

  string s;
  int n, i, a;
  list<int> li;
  list<int>::iterator p = li.begin();
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> a;
      li.push_front(a);
    } else if (s == "delete") {
      cin >> a;
      p = find(li.begin(), li.end(), a);
      if (p != li.end())
        li.erase(p);

    } else if (s == "deleteFirst") {
      li.pop_front();
    } else if (s == "deleteLast") {
      li.pop_back();
    }
  }
  p = li.begin();

  while (p != li.end()) {
    cout << *p;
    p++;
    if (p != li.end())
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s;
  int n, i, a;
  list<int> li;
  list<int>::iterator p = li.begin();
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> a;
      li.push_front(a);
    } else if (s == "delete") {
      cin >> a;
      p = find(li.begin(), li.end(), a);
      if (p != li.end())
        li.erase(p);

    } else if (s == "deleteFirst") {
      li.pop_front();
    } else if (s == "deleteLast") {
      li.pop_back();
    }
  }
  p = li.begin();

  while (p != li.end()) {
    cout << *p;
    p++;
    if (p != li.end())
      cout << " ";
    else
      cout << endl;
  }

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>

using namespace std;

void print_list(list<int> &V) {
  for (list<int>::iterator it = V.begin(); it != V.end(); it++) {
    if (it == V.begin()) {
      cout << *it;
    } else {
      cout << " " << *it;
    }
  }
  cout << endl;
}
int main(void) {
  list<int> V;

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;

    int x;
    if (s == "insert") {
      cin >> x;
      // V.(V.begin(), x);
      V.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      for (list<int>::iterator it = V.begin(); it != V.end(); it++) {
        if (*it == x) {
          V.erase(it);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      V.pop_front();
    } else {
      V.pop_back();
    }
  }
  print_list(V);

  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>

using namespace std;

void print_list(list<int> &V) {
  for (list<int>::iterator it = V.begin(); it != V.end(); it++) {
    if (it == V.begin()) {
      cout << *it;
    } else {
      cout << " " << *it;
    }
  }
  cout << endl;
}
int main(void) {
  ios_base::sync_with_stdio(false);

  list<int> V;

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;

    int x;
    if (s == "insert") {
      cin >> x;
      // V.(V.begin(), x);
      V.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      for (list<int>::iterator it = V.begin(); it != V.end(); it++) {
        if (*it == x) {
          V.erase(it);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      V.pop_front();
    } else {
      V.pop_back();
    }
  }
  print_list(V);

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

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

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

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

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

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

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

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

int main() {
  int n, key;
  char com[20];
  scanf("%d", &n);
  for (int 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);
      }
    }
  }
  Node *cur = nil->next;
  int isf = 0;
  while (1) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\n");
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

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

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

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

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

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

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

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

int main() {
  int n, key;
  char com[20];
  scanf("%d", &n);
  init();
  for (int 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);
      }
    }
  }
  Node *cur = nil->next;
  int isf = 0;
  while (1) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\n");
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <list>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define LEN 100005
using namespace std;
// list実装

int main() {
  list<int> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    if (c[0] == 'i') {
      int a;
      scanf("%d", &a);
      l.push_front(a);
    } else {
      if (c[6] == 'F') {
        l.pop_front();
      } else if (c[6] == 'L') {
        l.pop_back();
      } else {
        int a;
        scanf("%d", &a);
        for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) {
          if (*itr == a) {
            l.erase(itr);
            break;
          }
        }
      }
    }
  }
  int i = 0;
  for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) {
    if (i++)
      cout << " ";
    printf("%d", *itr);
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <list>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define LEN 100005
using namespace std;
// list実装

int main() {
  list<int> l;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    if (c[0] == 'i') {
      int a;
      scanf("%d", &a);
      l.push_front(a);
    } else {
      if (c[6] == 'F') {
        l.pop_front();
      } else if (c[6] == 'L') {
        l.pop_back();
      } else {
        int a;
        scanf("%d", &a);
        for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) {
          if (*itr == a) {
            l.erase(itr);
            break;
          }
        }
      }
    }
  }
  int i = 0;
  for (list<int>::iterator itr = l.begin(); itr != l.end(); itr++) {
    if (i++)
      cout << " ";
    printf("%d", *itr);
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef struct _list_ {
  struct _list_ *prev;
  int value;
  struct _list_ *next;
} list;

int main(void) {
  int max, count;
  vector<list> x(100000);
  string a;

  x[0].value = -1;
  x[1].value = -1;
  x[0].next = &x[1];
  x[1].prev = &x[0];

  count = 1;

  cin >> max;

  for (int i = 0; i < max; i++) {
    cin >> a;

    if (a == "insert") {
      count++;
      cin >> x[count].value;
      x[count].prev = &x[0];
      x[count].next = x[0].next;
      x[0].next->prev = &x[count];
      x[0].next = &x[count];
    }

    else if (a == "delete") {
      list *nextl = x[0].next;
      int b;
      cin >> b;
      for (;;) {
        if (nextl->value == b) {
          nextl->prev->next = nextl->next;
          nextl->next->prev = nextl->prev;
          break;
        }
        if (nextl->value == -1)
          break;
        nextl = nextl->next;
      }
    }

    else if (a == "deleteFirst") {
      x[0].next->next->prev = &x[0];
      x[0].next = x[0].next->next;
    }

    else if (a == "deleteLast") {
      x[1].prev->prev->next = &x[1];
      x[1].prev = x[1].prev->prev;
    }
  }

  list *nextl = x[0].next;

  if (nextl->value != -1) {
    cout << nextl->value;
    nextl = nextl->next;
  }

  for (;;) {
    if (nextl->value == -1)
      break;
    cout << " " << nextl->value;
    nextl = nextl->next;
  }

  cout << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef struct _list_ {
  struct _list_ *prev;
  int value;
  struct _list_ *next;
} list;

int main(void) {
  int max, count;
  vector<list> x(2000000);
  string a;

  x[0].value = -1;
  x[1].value = -1;
  x[0].next = &x[1];
  x[1].prev = &x[0];

  count = 1;

  cin >> max;

  for (int i = 0; i < max; i++) {
    cin >> a;

    if (a == "insert") {
      count++;
      cin >> x[count].value;
      x[count].prev = &x[0];
      x[count].next = x[0].next;
      x[0].next->prev = &x[count];
      x[0].next = &x[count];
    }

    else if (a == "delete") {
      list *nextl = x[0].next;
      int b;
      cin >> b;
      for (;;) {
        if (nextl->value == b) {
          nextl->prev->next = nextl->next;
          nextl->next->prev = nextl->prev;
          break;
        }
        if (nextl->value == -1)
          break;
        nextl = nextl->next;
      }
    }

    else if (a == "deleteFirst") {
      x[0].next->next->prev = &x[0];
      x[0].next = x[0].next->next;
    }

    else if (a == "deleteLast") {
      x[1].prev->prev->next = &x[1];
      x[1].prev = x[1].prev->prev;
    }
  }

  list *nextl = x[0].next;

  if (nextl->value != -1) {
    cout << nextl->value;
    nextl = nextl->next;
  }

  for (;;) {
    if (nextl->value == -1)
      break;
    cout << " " << nextl->value;
    nextl = nextl->next;
  }

  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  list<int> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    int k;
    cin >> s;
    if (s == "insert") {
      cin >> k;
      l.emplace_front(k);
    } else if (s == "delete") {
      cin >> k;
      auto it = find(l.begin(), l.end(), k);
      if (it == l.end())
        continue;
      l.erase(it);
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }

  for (auto it = l.begin(); it != l.end(); it++) {
    cout << *it;
    if (it != --l.end()) {
      cout << " ";
    }
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  list<int> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    int k;
    cin >> s;
    if (s == "insert") {
      cin >> k;
      l.emplace_front(k);
    } else if (s == "delete") {
      cin >> k;
      auto it = find(l.begin(), l.end(), k);
      if (it == l.end())
        continue;
      l.erase(it);
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }

  for (auto it = l.begin(); it != l.end(); it++) {
    cout << *it;
    if (it != --l.end()) {
      cout << " ";
    }
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <sstream>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int main() {
  list<int> data;
  list<int>::iterator it;
  int n;
  cin >> n;
  int x;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      data.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      for (it = data.begin(); it != data.end(); it++) {
        if (*it == x) {
          data.erase(it);
          break;
        }
      }
    } else if (str == "deleteFirst")
      data.pop_front();
    else
      data.pop_back();
  }
  it = data.begin();
  while (it != data.end()) {
    cout << *it;
    it++;
    (it == data.end()) ? cout << endl : cout << " ";
  }
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <sstream>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  list<int> data;
  list<int>::iterator it;
  int n;
  cin >> n;
  int x;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      data.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      for (it = data.begin(); it != data.end(); it++) {
        if (*it == x) {
          data.erase(it);
          break;
        }
      }
    } else if (str == "deleteFirst")
      data.pop_front();
    else
      data.pop_back();
  }
  it = data.begin();
  while (it != data.end()) {
    cout << *it;
    it++;
    (it == data.end()) ? cout << endl : cout << " ";
  }
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>
using namespace std;

void Command(list<int> &X, string &c) {
  if (c == "insert") {
    int d;
    cin >> d;
    X.push_front(d);
  } else if (c == "delete") {
    int d;
    cin >> d;
    list<int>::iterator itr = X.begin();
    while (*itr != d && itr != X.end()) {
      itr++;
    }
    if (itr != X.end()) {
      X.erase(itr);
    }
  } else if (c == "deleteFirst") {
    X.pop_front();
  } else if (c == "deleteLast") {
    X.pop_back();
  }
}

int main() {
  int n;
  cin >> n;
  list<int> X;
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    Command(X, c);
  }
  list<int>::iterator it = X.begin();
  list<int>::iterator end = X.end();
  end--;
  while (it != end) {
    cout << *it << ' ';
    it++;
  }
  cout << *end << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>
using namespace std;

void Command(list<int> &X, string &c) {
  if (c == "insert") {
    int d;
    cin >> d;
    X.push_front(d);
  } else if (c == "delete") {
    int d;
    cin >> d;
    list<int>::iterator itr = X.begin();
    while (*itr != d && itr != X.end()) {
      itr++;
    }
    if (itr != X.end()) {
      X.erase(itr);
    }
  } else if (c == "deleteFirst") {
    X.pop_front();
  } else if (c == "deleteLast") {
    X.pop_back();
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  list<int> X;
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    Command(X, c);
  }
  list<int>::iterator it = X.begin();
  list<int>::iterator end = X.end();
  end--;
  while (it != end) {
    cout << *it << ' ';
    it++;
  }
  cout << *end << endl;
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int len = 1000050;

inline int prev(int pos) { return (pos == 0) ? len - 1 : pos - 1; }

inline int next(int pos) { return (pos == len - 1) ? 0 : pos + 1; }

int main() {
  int n;
  cin >> n;
  vector<int> list(len);
  int first = 0, last = 0;

  string op;
  int key;
  for (int i = 0; i < n; i++) {
    cin >> op;
    if (op == "insert") {
      cin >> key;
      first = prev(first);
      list[first] = key;
    } else if (op == "deleteFirst") {
      while (list[first] == -1) {
        first = next(first);
      }
      first = next(first);
    } else if (op == "deleteLast") {
      while (list[prev(last)] == -1) {
        last = prev(last);
      }
      last = prev(last);
    } else {
      cin >> key;
      for (int j = first; j != last; j = next(j)) {
        if (list[j] == key) {
          list[j] = -1;
          break;
        }
      }
    }
  }

  bool nospace = true;
  for (int i = first; i != last; i = next(i)) {
    if (list[i] == -1)
      continue;
    if (nospace) {
      cout << list[i];
      nospace = false;
    } else {
      cout << " " << list[i];
    }
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int len = 1000050;

inline int prev(int pos) { return (pos == 0) ? len - 1 : pos - 1; }

inline int next(int pos) { return (pos == len - 1) ? 0 : pos + 1; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> list(len);
  int first = 0, last = 0;

  string op;
  int key;
  for (int i = 0; i < n; i++) {
    cin >> op;
    if (op == "insert") {
      cin >> key;
      first = prev(first);
      list[first] = key;
    } else if (op == "deleteFirst") {
      while (list[first] == -1) {
        first = next(first);
      }
      first = next(first);
    } else if (op == "deleteLast") {
      while (list[prev(last)] == -1) {
        last = prev(last);
      }
      last = prev(last);
    } else {
      cin >> key;
      for (int j = first; j != last; j = next(j)) {
        if (list[j] == key) {
          list[j] = -1;
          break;
        }
      }
    }
  }

  bool nospace = true;
  for (int i = first; i != last; i = next(i)) {
    if (list[i] == -1)
      continue;
    if (nospace) {
      cout << list[i];
      nospace = false;
    } else {
      cout << " " << list[i];
    }
  }
  cout << endl;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string str;
  list<int> data;
  list<int>::iterator it, l;
  int n, x;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      data.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      data.erase(find(data.begin(), data.end(), x));
    } else if (str == "deleteFirst")
      data.pop_front();
    else
      data.pop_back();
  }
  for (it = data.begin(), l = --data.end(); it != l; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string str;
  list<int> data;
  list<int>::iterator it, l;
  int n, x;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      data.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      it = find(data.begin(), data.end(), x);
      if (it != data.end())
        data.erase(it);
    } else if (str == "deleteFirst")
      data.pop_front();
    else
      data.pop_back();
  }
  for (it = data.begin(), l = --data.end(); it != l; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

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

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

Node *listsearch(int key) {
  Node *cur = nil->next;
  while (cur != NULL && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

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

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

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

void deleteKey(int key) { deleteNode(listsearch(key)); }

void print() {
  int j = 0;
  Node *cur = nil->next;
  while (1) {
    if (cur == nil) {
      break;
    }
    if (j++ > 0) {
      printf(" ");
    }
    printf("%d", cur->key);
    cur = cur->next;
  }
  putchar('\n');
}

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

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

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

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

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

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

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

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

void deleteKey(int key) { deleteNode(listsearch(key)); }

void print() {
  int j = 0;
  Node *cur = nil->next;
  while (1) {
    if (cur == nil) {
      break;
    }
    if (j++ > 0) {
      printf(" ");
    }
    printf("%d", cur->key);
    cur = cur->next;
  }
  putchar('\n');
}

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

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;
void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

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

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

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

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

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

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

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

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 (strlen(com) == 6)
      deleteKey(key);
    else if (com[6] == 'F')
      deleteFirst();
    else
      deleteLast();
  }

  printList();

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;
void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

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

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

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

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

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

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

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

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 (strlen(com) == 6)
      deleteKey(key);
    else if (com[6] == 'F')
      deleteFirst();
    else
      deleteLast();
  }

  printList();

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
signed main() {
  cin.tie(0);
  int n;
  cin >> n;
  list<int> l;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s;
    if (s == "insert" || s == "delete")
      cin >> x;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      l.pop_back();
      continue;
    }
    if (s == "insert") {
      l.push_front(x);
      continue;
    }
    if (s == "delete") {
      auto it = find(l.begin(), l.end(), x);
      if (it == l.end())
        continue;
      l.erase(it);
      continue;
    }
  }
  bool f = 0;
  for (auto it = l.begin(); it != l.end(); ++it) {
    if (f)
      cout << " ";
    cout << *it;
    f = 1;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  list<int> l;
  for (int i = 0; i < n; i++) {
    string s;
    int x;
    cin >> s;
    if (s == "insert" || s == "delete")
      cin >> x;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    }
    if (s == "deleteLast") {
      l.pop_back();
      continue;
    }
    if (s == "insert") {
      l.push_front(x);
      continue;
    }
    if (s == "delete") {
      auto it = find(l.begin(), l.end(), x);
      if (it == l.end())
        continue;
      l.erase(it);
      continue;
    }
  }
  bool f = 0;
  for (auto it = l.begin(); it != l.end(); ++it) {
    if (f)
      cout << " ";
    cout << *it;
    f = 1;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node {
  int key;
  Node *pre;
  Node *next;
};
Node *nil;
void init() {
  nil = new Node;
  nil->pre = nil;
  nil->next = nil;
}
void insert(int x) {
  Node *add = new Node;
  add->key = x;
  add->next = nil->next;
  nil->next->pre = add;
  nil->next = add;
  add->pre = nil;
  add->next;
}
void deleteNode(Node *d) {
  if (d == nil)
    return;
  Node *p = d->pre;
  Node *n = d->next;
  p->next = n;
  n->pre = p;
  free(d);
}
void deleteOne(int x) {
  Node *i = nil->next;
  while (i->key != x)
    i = i->next;
  deleteNode(i);
}
void deleteFirst() { deleteNode(nil->next); }
void deleteLast() { deleteNode(nil->pre); }
int main() {
  init();
  int len;
  cin >> len;
  for (int i = 0; i < len; i++) {
    char op[20];
    int x;
    scanf("%s", op);
    if (op[0] == 'i') {
      scanf("%d", &x);
      insert(x);
    } else if (op[0] == 'd') {
      if (strlen(op) <= 6) {
        scanf("%d", &x);
        deleteOne(x);
      } else if (op[6] == 'F')
        deleteFirst();
      else
        deleteLast();
    }
  }
  Node *item = nil->next;
  while (nil->pre != item) {
    printf("%d ", item->key);
    item = item->next;
  }
  printf("%d\n", item->key);
  return 0;
}


Fixed submission:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Node {
  int key;
  Node *pre;
  Node *next;
};
Node *nil;
void init() {
  nil = new Node;
  nil->pre = nil;
  nil->next = nil;
}
void insert(int x) {
  Node *add = new Node;
  add->key = x;
  add->next = nil->next;
  nil->next->pre = add;
  nil->next = add;
  add->pre = nil;
  add->next;
}
void deleteNode(Node *d) {
  if (d == nil)
    return;
  Node *p = d->pre;
  Node *n = d->next;
  p->next = n;
  n->pre = p;
  free(d);
}
void deleteOne(int x) {
  Node *i = nil->next;
  while (i != nil && i->key != x)
    i = i->next;
  deleteNode(i);
}
void deleteFirst() { deleteNode(nil->next); }
void deleteLast() { deleteNode(nil->pre); }
int main() {
  init();
  int len;
  cin >> len;
  for (int i = 0; i < len; i++) {
    char op[20];
    int x;
    scanf("%s", op);
    if (op[0] == 'i') {
      scanf("%d", &x);
      insert(x);
    } else if (op[0] == 'd') {
      if (strlen(op) <= 6) {
        scanf("%d", &x);
        deleteOne(x);
      } else if (op[6] == 'F')
        deleteFirst();
      else
        deleteLast();
    }
  }
  Node *item = nil->next;
  while (nil->pre != item) {
    printf("%d ", item->key);
    item = item->next;
  }
  printf("%d\n", item->key);
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  list<int> l;
  int n;
  string cmd;
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (cmd == "delete") {
      cin >> x;
      l.erase(find(begin(l), end(l), x));
    } else if (cmd == "deleteFirst") {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }
  for (auto it = l.begin(); it != l.end();) {
    cout << *it;
    it++;
    if (it == l.end()) {
      cout << endl;
    } else {
      cout << ' ';
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  list<int> l;
  int n;
  string cmd;
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (cmd == "delete") {
      cin >> x;
      auto it = find(begin(l), end(l), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (cmd == "deleteFirst") {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }
  for (auto it = l.begin(); it != l.end();) {
    cout << *it;
    it++;
    if (it == l.end()) {
      cout << endl;
    } else {
      cout << ' ';
    }
  }
}