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 command;
    cin >> command;
    if (command == "insert") {
      int x;
      cin >> x;
      l.push_front(x);
    } else if (command == "delete") {
      int x;
      cin >> x;
      for (list<int>::iterator i = l.begin(); i != l.end(); i++) {
        if (*i == x) {
          l.erase(i);
          break;
        }
      }
    } else if (command == "deleteFirst") {
      l.pop_front();
    } else if (command == "deleteLast") {
      l.pop_back();
    }
  }

  list<int>::iterator i;
  for (i = l.begin(); i != --l.end(); i++) {
    cout << *i << " ";
  }
  cout << *i << endl;

  return 0;
}


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

using namespace std;

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

  int n;
  cin >> n;

  list<int> l;
  for (int i = 0; i < n; i++) {
    string command;
    cin >> command;
    if (command == "insert") {
      int x;
      cin >> x;
      l.push_front(x);
    } else if (command == "delete") {
      int x;
      cin >> x;
      for (list<int>::iterator i = l.begin(); i != l.end(); i++) {
        if (*i == x) {
          l.erase(i);
          break;
        }
      }
    } else if (command == "deleteFirst") {
      l.pop_front();
    } else if (command == "deleteLast") {
      l.pop_back();
    }
  }

  list<int>::iterator i;
  for (i = l.begin(); i != --l.end(); i++) {
    cout << *i << " ";
  }
  cout << *i << endl;

  return 0;
}
Problem p02265 (C++)

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

int main() {
  int n, i, x;
  string s;
  deque<int> intlist;
  cin >> n;

  for (i = 0; i <= n - 1; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      intlist.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      deque<int>::iterator it = intlist.begin();
      while (1) {
        if (*it == x) {
          it = intlist.erase(it);
          break;
        } else if (it == intlist.end()) {
          break;
        }
        it++;
      }
    } else if (s == "deleteFirst") {
      intlist.pop_front();
    } else if (s == "deleteLast") {
      intlist.pop_back();
    }
  }

  deque<int>::iterator it = intlist.begin();
  cout << *it;
  it++;
  while (1) {
    if (it == intlist.end()) {
      cout << "\n";
      break;
    }
    cout << " " << *it;
    it++;
  }

  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, i, x;
  string s;
  deque<int> intlist;
  cin >> n;

  for (i = 0; i <= n - 1; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      intlist.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      deque<int>::iterator it = intlist.begin();
      while (1) {
        if (*it == x) {
          it = intlist.erase(it);
          break;
        } else if (it == intlist.end()) {
          break;
        }
        it++;
      }
    } else if (s == "deleteFirst") {
      intlist.pop_front();
    } else if (s == "deleteLast") {
      intlist.pop_back();
    }
  }

  deque<int>::iterator it = intlist.begin();
  cout << *it;
  it++;
  while (1) {
    if (it == intlist.end()) {
      cout << "\n";
      break;
    }
    cout << " " << *it;
    it++;
  }

  return 0;
}
Problem p02265 (C++)

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

int main() {
  int n;
  cin >> n;
  list<int> l;
  string s;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      list<int>::iterator it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

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

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

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  list<int> l;
  string s;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    if (s == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      cin >> x;
      list<int>::iterator it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

  for (list<int>::iterator it = l.begin(); it != l.end(); ++it) {
    if (it != l.begin())
      cout << ' ';
    cout << *it;
  }
  cout << endl;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
struct lis {
  int d;
  lis *pr;
  lis *ne;
};
lis he;
int n;
void ins();
void del();
void delf();
void dell();
int main() {
  cin >> n;
  string ord;
  lis bu = {-1, &he, &he};
  he = bu;
  for (int i = 0; i < n; i++) {

    cin >> ord;

    if (ord == "insert")
      ins();
    else if (ord == "delete")
      del();
    else if (ord == "deleteFirst")
      delf();
    else if (ord == "deleteLast")
      dell();
  }
  for (lis *i = he.ne; i != &he; i = i->ne) {
    cout << i->d;
    if (i->ne != &he) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}
void ins() {
  int x;
  cin >> x;
  lis *p;
  p = new lis;
  p->d = x;
  p->ne = he.ne;
  p->pr = &he;
  he.ne->pr = p;
  he.ne = p;
}
void del() {
  int x;
  cin >> x;
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->d == x) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
      return;
    }
  }
}
void dell() {
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->ne == &he) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
    }
  }
}
void delf() {
  lis *i = he.ne;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct lis {
  int d;
  lis *pr;
  lis *ne;
};
lis he;
int n;
void ins();
void del();
void delf();
void dell();
int main() {
  cin >> n;
  string ord;
  lis bu = {-1, &he, &he};
  he = bu;
  for (int i = 0; i < n; i++) {

    cin >> ord;

    if (ord == "insert")
      ins();
    else if (ord == "delete")
      del();
    else if (ord == "deleteFirst")
      delf();
    else if (ord == "deleteLast")
      dell();
  }
  for (lis *i = he.ne; i != &he; i = i->ne) {
    cout << i->d;
    if (i->ne != &he) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}
void ins() {
  int x;
  cin >> x;
  lis *p;
  p = new lis;
  p->d = x;
  p->ne = he.ne;
  p->pr = &he;
  he.ne->pr = p;
  he.ne = p;
}
void del() {
  int x;
  cin >> x;
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->d == x) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
      return;
    }
  }
}
void dell() {
  lis *i = he.pr;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}
void delf() {
  lis *i = he.ne;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  int n, x;
  string str;
  list<int> lst;
  list<int>::iterator it;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      lst.push_front(x);
    } else if (str == "deleteFirst")
      lst.pop_front();
    else if (str == "deleteLast")
      lst.pop_back();

    else {
      cin >> x;
      for (it = lst.begin(); it != lst.end(); ++it) {
        if (*it == x) {
          lst.erase(it);
          break;
        }
      }
    }
  }
  for (it = lst.begin(); it != --lst.end(); ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, x;
  string str;
  list<int> lst;
  list<int>::iterator it;
  cin >> n;
  while (n--) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      lst.push_front(x);
    } else if (str == "deleteFirst")
      lst.pop_front();
    else if (str == "deleteLast")
      lst.pop_back();

    else {
      cin >> x;
      for (it = lst.begin(); it != lst.end(); ++it) {
        if (*it == x) {
          lst.erase(it);
          break;
        }
      }
    }
  }
  for (it = lst.begin(); it != --lst.end(); ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
  return 0;
}
Problem p02265 (C++)

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

struct DLL {
  ll x;
  DLL *next;
  DLL *prev;
};

int main(void) {
  int n;
  cin >> n;
  DLL *head;
  head = (DLL *)malloc(sizeof(DLL));
  head->x = -1;
  head->next = head;
  head->prev = head;
  for (int i = 0; i < n; i++) {
    string comm;
    ll v;
    cin >> comm;
    if (comm == "insert") {
      cin >> v;
      DLL *x;
      x = (DLL *)malloc(sizeof(DLL));
      x->x = v;
      x->next = head->next;
      head->next->prev = x;
      head->next = x;
      x->prev = head;
    }
    if (comm == "delete") {
      cin >> v;
      DLL *key;
      key = head->next;
      while (1) {
        if (key == head)
          break;
        if (key->x == v) {
          key->prev->next = key->next;
          key->next->prev = key->prev;
          free(key);
          break;
        }
        key = key->next;
      }
    }
    if (comm == "deleteFirst") {
      DLL *key = head->next;
      key->prev->next = key->next;
      key->next->prev = key->prev;
      free(key);
    }
    if (comm == "deleteLast") {
      DLL *key = head->prev;
      key->prev->next = key->next;
      key->next->prev = key->prev;
      free(key);
    }
  }

  DLL *a;
  a = head->next;
  while (a != head) {
    if (a->next == head)
      cout << a->x << endl;
    else
      cout << a->x << " ";
    a = a->next;
  }

  return 0;
}

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

struct DLL {
  ll x;
  DLL *next;
  DLL *prev;
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  DLL *head;
  head = (DLL *)malloc(sizeof(DLL));
  head->x = -1;
  head->next = head;
  head->prev = head;
  for (int i = 0; i < n; i++) {
    string comm;
    ll v;
    cin >> comm;
    if (comm == "insert") {
      cin >> v;
      DLL *x;
      x = (DLL *)malloc(sizeof(DLL));
      x->x = v;
      x->next = head->next;
      head->next->prev = x;
      head->next = x;
      x->prev = head;
    }
    if (comm == "delete") {
      cin >> v;
      DLL *key;
      key = head->next;
      while (1) {
        if (key == head)
          break;
        if (key->x == v) {
          key->prev->next = key->next;
          key->next->prev = key->prev;
          free(key);
          break;
        }
        key = key->next;
      }
    }
    if (comm == "deleteFirst") {
      DLL *key = head->next;
      key->prev->next = key->next;
      key->next->prev = key->prev;
      free(key);
    }
    if (comm == "deleteLast") {
      DLL *key = head->prev;
      key->prev->next = key->next;
      key->next->prev = key->prev;
      free(key);
    }
  }

  DLL *a;
  a = head->next;
  while (a != head) {
    if (a->next == head)
      cout << a->x << endl;
    else
      cout << a->x << " ";
    a = a->next;
  }

  return 0;
}
Problem p02265 (C++)

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

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

using namespace std;

int n;
list<int> x;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  rep(i, n) {
    string a;
    cin >> a;
    if (a == "deleteFirst") {
      x.pop_front();
    } else if (a == "deleteLast") {
      x.pop_back();
    } else {
      int b;
      cin >> b;
      if (a == "insert") {
        x.push_front(b);
      } else {
        x.erase(find(x.begin(), x.end(), b));
      }
    }
  }
  for (list<int>::iterator it = x.begin(); it != x.end(); ++it) {
    if (it != x.begin()) {
      cout << ' ';
    }
    cout << *it;
  }
  cout << endl;
  return 0;
}

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

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

using namespace std;

int n;
list<int> x;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  rep(i, n) {
    string a;
    cin >> a;
    if (a == "deleteFirst") {
      x.pop_front();
    } else if (a == "deleteLast") {
      x.pop_back();
    } else {
      int b;
      cin >> b;
      if (a == "insert") {
        x.push_front(b);
      } else {
        list<int>::iterator it = find(x.begin(), x.end(), b);
        if (it != x.end()) {
          x.erase(it);
        }
      }
    }
  }
  for (list<int>::iterator it = x.begin(); it != x.end(); ++it) {
    if (it != x.begin()) {
      cout << ' ';
    }
    cout << *it;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

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

void display(list<int> &lst) {
  if (!lst.empty()) {
    cout << lst.front();
    lst.pop_front();
  }
  for (auto element : lst) {
    cout << ' ' << element;
  }
  cout << endl;
}

int main() {
  list<int> lst;
  int n = 0;
  cin >> n;
  for (int i = 0; i != n; ++i) {
    string command;
    cin >> command;
    if (command == "insert") {
      int x = 0;
      cin >> x;
      lst.push_front(x);
    } else if (command == "delete") {
      int x = 0;
      cin >> x;
      for (auto it = lst.begin(); it != lst.end(); ++it) {
        if (*it == x) {
          lst.erase(it);
          break;
        }
      }
    } else if (command == "deleteFirst") {
      lst.pop_front();
    } else if (command == "deleteLast") {
      lst.pop_back();
    }
  }
  display(lst);
  return 0;
}

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

void display(list<int> &lst) {
  if (!lst.empty()) {
    cout << lst.front();
    lst.pop_front();
  }
  for (auto element : lst) {
    cout << ' ' << element;
  }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  list<int> lst;
  int n = 0;
  cin >> n;
  for (int i = 0; i != n; ++i) {
    string command;
    cin >> command;
    if (command == "insert") {
      int x = 0;
      cin >> x;
      lst.push_front(x);
    } else if (command == "delete") {
      int x = 0;
      cin >> x;
      for (auto it = lst.begin(); it != lst.end(); ++it) {
        if (*it == x) {
          lst.erase(it);
          break;
        }
      }
    } else if (command == "deleteFirst") {
      lst.pop_front();
    } else if (command == "deleteLast") {
      lst.pop_back();
    }
  }
  display(lst);
  return 0;
}
Problem p02265 (C++)

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

int main(void) {
  list<int> l;
  int n;
  string s;
  int d;

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

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

  return 0;
}

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

int main(void) {
  std::ios_base::sync_with_stdio(false);
  list<int> l;
  int n;
  string s;
  int d;

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

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

  return 0;
}
Problem p02265 (C++)

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

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

using namespace std;

int main() {
  int n;
  list<int> l;
  cin >> n;
  REP(i, n) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      int x;
      cin >> x;
      l.push_front(x);
    } else {
      if (s[6] == 'F') {
        l.pop_front();
      } else if (s[6] == 'L') {
        l.pop_back();
      } else {
        int x;
        cin >> x;
        for (auto itr = l.begin(); itr != l.end(); ++itr) {
          if (*itr == x) {
            l.erase(itr);
            break;
          }
        }
      }
    }
  }
  int i = 0;

  for (auto itr = l.begin(); itr != l.end(); ++itr) {
    if (i++)
      cout << " ";
    cout << *itr;
  }
  cout << endl;
  return 0;
}

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

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

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  list<int> l;
  cin >> n;
  REP(i, n) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      int x;
      cin >> x;
      l.push_front(x);
    } else {
      if (s[6] == 'F') {
        l.pop_front();
      } else if (s[6] == 'L') {
        l.pop_back();
      } else {
        int x;
        cin >> x;
        for (auto itr = l.begin(); itr != l.end(); ++itr) {
          if (*itr == x) {
            l.erase(itr);
            break;
          }
        }
      }
    }
  }
  int i = 0;

  for (auto itr = l.begin(); itr != l.end(); ++itr) {
    if (i++)
      cout << " ";
    cout << *itr;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

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

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

Node *nil;

void insert_cell(int key) {
  Node *x;
  x = new Node;
  x->key = key;
  x->prev = nil;
  x->next = nil->next;
  x->next->prev = x;
  nil->next = x;
}

void deleteNode(Node *t) {
  if (t != nil) {
    t->prev->next = t->next;
    t->next->prev = t->prev;
    t->prev = NULL;
    t->next = NULL;
    delete t;
  }
}

void delete_cell(int x) {
  Node *cell;
  cell = new Node;
  cell = nil->next;
  while (cell->key != x)
    cell = cell->next;
  deleteNode(cell);
}

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

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

int main() {
  nil = new Node;
  nil->prev = nil;
  nil->next = nil;
  int n, k;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[20], c;
    scanf("%s%d", s, &k);
    if (s[0] == 'i') {
      insert_cell(k);
    } else {
      if (s[6] == 'F')
        deleteFirst();
      else if (s[6] == 'L')
        deleteLast();
      else
        delete_cell(k);
    }
  }
  Node *p;
  p = new Node;
  p = nil->next;
  for (int i = 0; p != nil; i++, p = p->next)
    if (i)
      cout << " " << p->key;
    else
      cout << p->key;
  delete p;
  cout << endl;
}

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

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

Node *nil;

void insert_cell(int key) {
  Node *x;
  x = new Node;
  x->key = key;
  x->prev = nil;
  x->next = nil->next;
  x->next->prev = x;
  nil->next = x;
}

void deleteNode(Node *t) {
  if (t != nil) {
    t->prev->next = t->next;
    t->next->prev = t->prev;
    t->prev = NULL;
    t->next = NULL;
    delete t;
  }
}

void delete_cell(int x) {
  Node *cell;
  cell = new Node;
  cell = nil->next;
  while (cell->key != x && cell != nil)
    cell = cell->next;
  deleteNode(cell);
}

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

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

int main() {
  nil = new Node;
  nil->prev = nil;
  nil->next = nil;
  int n, k;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[20], c;
    scanf("%s%d", s, &k);
    if (s[0] == 'i') {
      insert_cell(k);
    } else {
      if (s[6] == 'F')
        deleteFirst();
      else if (s[6] == 'L')
        deleteLast();
      else
        delete_cell(k);
    }
  }
  Node *p;
  p = new Node;
  p = nil->next;
  for (int i = 0; p != nil; i++, p = p->next)
    if (i)
      cout << " " << p->key;
    else
      cout << p->key;
  delete p;
  cout << endl;
}
Problem p02265 (C++)

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

using namespace std;

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

  list<int> l;
  string s;
  int x;
  while (n--) {
    cin >> s;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    } else if (s == "deleteLast") {
      l.pop_back();
      continue;
    }

    cin >> x;
    if (s == "insert")
      l.push_front(x);
    else if (s == "delete")
      l.erase(find(l.begin(), l.end(), x));
  }

  cout << l.front(), l.pop_front();
  for (auto it = l.begin(); it != l.end(); ++it)
    cout << " " << *it;
  cout << endl;

  return 0;
}

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

using namespace std;

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

  list<int> l;
  string s;
  int x;
  while (n--) {
    cin >> s;
    if (s == "deleteFirst") {
      l.pop_front();
      continue;
    } else if (s == "deleteLast") {
      l.pop_back();
      continue;
    }

    cin >> x;
    if (s == "insert")
      l.push_front(x);
    else if (s == "delete") {
      list<int>::iterator it = find(l.begin(), l.end(), x);
      if (it != l.end())
        l.erase(it);
    }
  }

  cout << l.front(), l.pop_front();
  for (auto it = l.begin(); it != l.end(); ++it)
    cout << " " << *it;
  cout << endl;

  return 0;
}
Problem p02265 (C++)

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

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

Node *NIL;

void init() {
  NIL = new Node();
  NIL->prev = NIL;
  NIL->next = NIL;
}

void insert(int key) {
  Node *x = new 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->key != key && cur != NIL) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *tmp) {
  tmp->next->prev = tmp->prev;
  tmp->prev->next = tmp->next;
  delete tmp;
}

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

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

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

void print() {
  Node *cur = NIL->next;
  while (cur != NIL) {
    if (cur != NIL->next)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\n");
}

int main() {
  int n;
  char S[20];
  int key;

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

  print();
  return 0;
}

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

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

Node *NIL;

void init() {
  NIL = new Node();
  NIL->prev = NIL;
  NIL->next = NIL;
}

void insert(int key) {
  Node *x = new 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->key != key && cur != NIL) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *tmp) {
  if (tmp == NIL)
    return;
  tmp->next->prev = tmp->prev;
  tmp->prev->next = tmp->next;
  delete tmp;
}

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

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

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

void print() {
  Node *cur = NIL->next;
  while (cur != NIL) {
    if (cur != NIL->next)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\n");
}

int main() {
  int n;
  char S[20];
  int key;

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

  print();
  return 0;
}
Problem p02265 (C++)

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

int main() {
  std::list<unsigned> l;

  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l]() {
         unsigned i;
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l]() {
         unsigned i;
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::list<unsigned> l;

  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l]() {
         unsigned i;
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l]() {
         unsigned i;
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}
Problem p02265 (C++)

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

int main() {
  std::list<unsigned> l;

  unsigned i;
  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l, &i]() {
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l, &i]() {
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::list<unsigned> l;

  unsigned i;
  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l, &i]() {
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l, &i]() {
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}
Problem p02265 (C++)

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

class TDllist {
  std::list<int> FList;

public:
  void Insert(int X) { FList.push_front(X); }
  void Delete(int X) {
    for (std::list<int>::iterator It = FList.begin(); It != FList.end(); ++It) {
      if (*It == X) {
        FList.erase(It);
        return;
      }
    }
  }
  void DeleteFirst() { FList.pop_front(); }
  void DeleteLast() { FList.pop_back(); }
  void Show() const {
    std::list<int>::const_iterator It = FList.begin();
    std::cout << *It;
    for (++It; It != FList.end(); ++It) {
      std::cout << " " << *It;
    }
    std::cout << std::endl;
  }
};

int main() {
  std::cin.tie(0);
  TDllist List;
  int n, X;
  std::cin >> n;
  std::string Command;
  for (int i = 0; i < n; ++i) {
    // std::string Command;
    std::cin >> Command;
    if (Command == "insert") {
      // int X;
      std::cin >> X;
      List.Insert(X);
    } else if (Command == "delete") {
      // int X;
      std::cin >> X;
      List.Delete(X);
    } else if (Command == "deleteFirst") {
      List.DeleteFirst();
    } else if (Command == "deleteLast") {
      List.DeleteLast();
    } else {
      assert(false);
    }
  }
  List.Show();
  return 0;
}

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

class TDllist {
  std::list<int> FList;

public:
  void Insert(int X) { FList.push_front(X); }
  void Delete(int X) {
    for (std::list<int>::iterator It = FList.begin(); It != FList.end(); ++It) {
      if (*It == X) {
        FList.erase(It);
        return;
      }
    }
  }
  void DeleteFirst() { FList.pop_front(); }
  void DeleteLast() { FList.pop_back(); }
  void Show() const {
    std::list<int>::const_iterator It = FList.begin();
    std::cout << *It;
    for (++It; It != FList.end(); ++It) {
      std::cout << " " << *It;
    }
    std::cout << std::endl;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  TDllist List;
  int n, X;
  std::cin >> n;
  std::string Command;
  for (int i = 0; i < n; ++i) {
    // std::string Command;
    std::cin >> Command;
    if (Command == "insert") {
      // int X;
      std::cin >> X;
      List.Insert(X);
    } else if (Command == "delete") {
      // int X;
      std::cin >> X;
      List.Delete(X);
    } else if (Command == "deleteFirst") {
      List.DeleteFirst();
    } else if (Command == "deleteLast") {
      List.DeleteLast();
    } else {
      assert(false);
    }
  }
  List.Show();
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

int main() {
  deque<int> deq;
  int N;
  cin >> N;

  while (N--) {
    string cmd;
    cin >> cmd;
    if (cmd == "insert") {
      int x;
      cin >> x;
      deq.push_front(x);
    } else if (cmd == "delete") {
      int x;
      cin >> x;
      auto it = find(deq.begin(), deq.end(), x);
      if (it != deq.end())
        deq.erase(it);
    }
    if (cmd == "deleteFirst")
      deq.pop_front();
    if (cmd == "deleteLast")
      deq.pop_back();
  }

  if (!deq.empty()) {
    cout << deq.front();
    for (auto it = deq.begin() + 1; it != deq.end(); ++it)
      cout << " " << *it;
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

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

  deque<int> deq;
  int N;
  cin >> N;

  while (N--) {
    string cmd;
    cin >> cmd;
    if (cmd == "insert") {
      int x;
      cin >> x;
      deq.push_front(x);
    } else if (cmd == "delete") {
      int x;
      cin >> x;
      auto it = find(deq.begin(), deq.end(), x);
      if (it != deq.end())
        deq.erase(it);
    }
    if (cmd == "deleteFirst")
      deq.pop_front();
    if (cmd == "deleteLast")
      deq.pop_back();
  }

  if (!deq.empty()) {
    cout << deq.front();
    for (auto it = deq.begin() + 1; it != deq.end(); ++it)
      cout << " " << *it;
    cout << endl;
  }

  return 0;
}
Problem p02265 (C++)

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

using namespace std;
int main() {
  ios::sync_with_stdio(false);
  list<int> lis;
  int cnt;
  cin >> cnt;
  string tmp;
  int tmpI;
  for (int i = 0; i < cnt; i++) {
    cin >> tmp;
    if (tmp[0] == 'i') { // insert x
      cin >> tmpI;
      lis.push_front(tmpI);
    } else if (tmp == "delete") { // delete x
      cin >> tmpI;
      for (list<int>::iterator it = lis.begin(); it != lis.end(); i++) {
        if (*it == tmpI) {
          lis.erase(it);
          break;
        }
      }
    } else if (tmp[6] == 'F') { // deleteF
      lis.pop_front();
    } else { // deleteL
      lis.pop_back();
    }
  }
  for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) {
    if (it != lis.begin()) {
      cout << " ";
    }
    cout << *it;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
int main() {
  ios::sync_with_stdio(false);
  list<int> lis;
  int cnt;
  cin >> cnt;
  string tmp;
  int tmpI;
  for (int i = 0; i < cnt; i++) {
    cin >> tmp;
    if (tmp[0] == 'i') { // insert x
      cin >> tmpI;
      lis.push_front(tmpI);
    } else if (tmp == "delete") { // delete x
      cin >> tmpI;
      for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) {
        if (*it == tmpI) {
          lis.erase(it);
          break;
        }
      }
    } else if (tmp[6] == 'F') { // deleteF
      lis.pop_front();
    } else { // deleteL
      lis.pop_back();
    }
  }
  for (list<int>::iterator it = lis.begin(); it != lis.end(); it++) {
    if (it != lis.begin()) {
      cout << " ";
    }
    cout << *it;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

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

int main(void) {
  std::ios_base::sync_with_stdio(false);
  list<int> l;
  string s;
  int d;
  int n;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> d;
      l.push_front(d);
    } else if (s == "delete") {
      cin >> d;
      list<int>::iterator pos;
      pos = find(l.begin(), l.end(), d);
      l.erase(pos);
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

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

  return 0;
}

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

int main(void) {
  std::ios_base::sync_with_stdio(false);
  list<int> l;
  string s;
  int d;
  int n;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> d;
      l.push_front(d);
    } else if (s == "delete") {
      cin >> d;
      list<int>::iterator pos;
      pos = find(l.begin(), l.end(), d);
      if (pos != l.end())
        l.erase(pos);
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

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

  return 0;
}
Problem p02265 (C++)

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

int main() {
  int n, x;
  string cmd;
  list<int> alist;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> x;
      alist.push_front(x);
    } else if (cmd == "delete") {
      cin >> x;
      list<int>::iterator ait = alist.begin();
      while (1) {
        if (*ait == x) {
          alist.erase(ait);
          break;
        } else
          ait++;
      }
    } else if (cmd == "deleteFirst")
      alist.pop_front();
    else
      alist.pop_back();
  }
  n = alist.size();
  list<int>::iterator ait = alist.begin();
  for (int i = 0; i < n - 1; i++)
    cout << *ait++ << ' ';
  cout << *ait << endl;
  return 0;
}

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

int main() {
  int n, x;
  string cmd;
  list<int> alist;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "insert") {
      cin >> x;
      alist.push_front(x);
    } else if (cmd == "delete") {
      cin >> x;
      list<int>::iterator ait = alist.begin();
      while (ait != alist.end()) {
        if (*ait == x) {
          alist.erase(ait);
          break;
        } else
          ait++;
      }
    } else if (cmd == "deleteFirst")
      alist.pop_front();
    else
      alist.pop_back();
  }
  n = alist.size();
  list<int>::iterator ait = alist.begin();
  for (int i = 0; i < n - 1; i++)
    cout << *ait++ << ' ';
  cout << *ait << endl;
  return 0;
}
Problem p02265 (C++)

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

typedef struct _List {
  _List *front;
  _List *rear;
  int data;
} List;

int main(void) {
  std::ios_base::sync_with_stdio(false);
  int n;
  string s;
  int d;
  cin >> n;
  List *top;
  List *end;
  List a;
  a.front = NULL;
  a.rear = NULL;
  a.data = 0;
  top = &a;
  end = &a;

  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> d;
      List *temp = new List;

      temp->data = d;
      temp->rear = top;
      temp->front = NULL;
      top->front = temp;
      top = temp;
    } else if (s == "delete") {
      cin >> d;
      List *t = top;

      while (t != NULL && t->rear != NULL) {
        if (t->data == d) {
          if (t->front != NULL)
            (t->front)->rear = (t->rear);
          else
            top = t->rear;
          if (t->rear != NULL)
            (t->rear)->front = (t->front);
          break;
        }
        t = t->rear;
      }
    } else if (s == "deleteFirst") {
      if (top->rear != NULL)
        (top->rear)->front = NULL;
      top = top->rear;
    } else if (s == "deleteLast") {
      List *t = end;

      t = t->front;
      t = t->front;
      t->rear = end;
      end->front = t;
    }
  }

  List *t = top;
  while (t->rear != NULL) {
    if (t != top)
      cout << " ";
    cout << t->data;
    t = t->rear;
  }
  cout << endl;

  return 0;
}

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

typedef struct _List {
  _List *front;
  _List *rear;
  int data;
} List;

int main(void) {
  std::ios_base::sync_with_stdio(false);
  int n;
  string s;
  int d;
  cin >> n;
  List *top;
  List *end;
  List a;
  a.front = NULL;
  a.rear = NULL;
  a.data = 0;
  top = &a;
  end = &a;

  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> d;
      List *temp = new List;

      temp->data = d;
      temp->rear = top;
      temp->front = NULL;
      top->front = temp;
      top = temp;
    } else if (s == "delete") {
      cin >> d;
      List *t = top;

      while (t != NULL && t->rear != NULL) {
        if (t->data == d) {
          if (t->front != NULL)
            (t->front)->rear = (t->rear);
          else
            top = t->rear;
          if (t->rear != NULL)
            (t->rear)->front = (t->front);
          break;
        }
        t = t->rear;
      }
    } else if (s == "deleteFirst") {
      if (top->rear != NULL)
        (top->rear)->front = NULL;
      top = top->rear;
    } else if (s == "deleteLast") {
      List *t = end;

      t = t->front;
      t = t->front;
      if (t != NULL)
        t->rear = end;
      else
        top = end;
      end->front = t;
    }
  }

  List *t = top;
  while (t->rear != NULL) {
    if (t != top)
      cout << " ";
    cout << t->data;
    t = t->rear;
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

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

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->next;
  nil->next = x;
  x->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)); }

int main() {
  int key, n, i;
  int size = 0;
  char com[20];
  int np = 0, nd = 0;
  scanf("%d", &n);

  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i') {
      insert(key);
      np++;
      size++;
    } else if (com[0] == 'd') {
      if (strlen(com) > 6) {
        if (com[6] == 'F')
          deleteFirst();
        else if (com[6] == 'L')
          deleteLast();
      } else {
        deleteKey(key);
        nd++;
      }
      size--;
    }
  }
  printList();
  return 0;
}


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

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;
}

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)); }

int main() {
  int key, n, i;
  int size = 0;
  char com[20];
  int np = 0, nd = 0;
  scanf("%d", &n);

  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", com, &key);
    if (com[0] == 'i') {
      insert(key);
      np++;
      size++;
    } else if (com[0] == 'd') {
      if (strlen(com) > 6) {
        if (com[6] == 'F')
          deleteFirst();
        else if (com[6] == 'L')
          deleteLast();
      } else {
        deleteKey(key);
        nd++;
      }
      size--;
    }
  }
  printList();
  return 0;
}
Problem p02265 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

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

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;

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 comm;
    int x;
    cin >> comm;
    if (comm == "insert") {
      cin >> x;
      l.push_front(x);
    } else if (comm == "delete") {
      cin >> x;
      for (auto it = l.begin(); it != l.end(); it++) {
        if (*it == x) {
          l.erase(it);
          break;
        }
      }
    } else if (comm == "deleteFirst") {
      l.pop_front();
    } else {
      l.pop_back();
    }
  }
  for (auto it = l.begin(); it != l.end(); it++) {
    if (it != l.begin())
      cout << ' ';
    cout << *it;
  }
  cout << endl;

  return 0;
}