alexjercan/bugnet huggingface dataset:
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;

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

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

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 s[20];
  scanf("%d", &n);
  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", s, &key);
    if (s[0] == 'i') {
      insert(key);
    } else if (s[0] == 'd') {
      if (strlen(s) > 6) {
        if (s[6] == 'F') {
          deletefirst();
        } else if (s[6] == 'L') {
          deletelast();
        }
      } else
        deletekey(key);
    }
  }
  printlist();
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;

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

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);
  }
  printf("\\n");
}

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 s[20];
  scanf("%d", &n);
  init();
  for (i = 0; i < n; i++) {
    scanf("%s%d", s, &key);
    if (s[0] == 'i') {
      insert(key);
    } else if (s[0] == 'd') {
      if (strlen(s) > 6) {
        if (s[6] == 'f') {
          deletefirst;
        } else if (s[6] == 'l') {
          deletelast;
        }
      } else
        deletekey(key);
    }
  }
  printlist();
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Runtime Error

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

struct command {
  char com[20];
  int key;
};
struct Node {
  int key;
  Node *next, *prev;
};
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)); }

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

int main() {
  int n;
  scanf("%d", &n);
  command G;
  init();
  for (int i = 0; i < n; i++) {

    scanf("%s%d", G.com, &G.key);
    if (G.com[0] == 'i') {
      insert(G.key);
    } else if (strlen(G.com) < 8) {
      deleteKey(G.key);
    } else if (G.com[6] == 'F') {
      deleteFirst();
    } else {
      deleteLast();
    }
  }
  printList();
  return 0;
}

Error: -11

Fixed submission:

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

struct command {
  char com[20];
  int key;
};
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)); }

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

int main() {
  int n;
  scanf("%d", &n);
  command G;
  for (int i = 0; i < n; i++) {
    scanf("%s%d", G.com, &G.key);
    if (G.com[0] == 'i') {
      insert(G.key);
    } else if (strlen(G.com) < 8) {
      deleteKey(G.key);
    } else if (G.com[6] == 'F') {
      deleteFirst();
    } else {
      deleteLast();
    }
  }
  printList();
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: 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") {
      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;
}

Error: 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")
      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;
}
Problem p02265 (C++) — buggy submission, verdict: 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 != 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;
}

Error: TLE

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 = 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, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <list>
using namespace std;
typedef long long ll;

int main() {
  int n, i, a;
  char s[20];
  list<int> v;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &a);
      v.push_front(a);
    } else if (s[6] == 'F')
      v.pop_front();
    else if (s[6] == 'L')
      v.pop_back();
    else {
      scanf("%d", &a);
      for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
        if (*it == a) {
          v.erase(it);
          break;
        }
      }
    }
  }
  i = 0;
  for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
    if (i++)
      printf(" ");
    printf("%d", *it);
  }
  putchar('\\n');
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <list>
using namespace std;
typedef long long ll;

int main() {
  int n, i, a;
  char s[20];
  list<int> v;

  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%lld", &a);
      v.push_front(a);
    } else if (s[6] == 'F')
      v.pop_front();
    else if (s[6] == 'L')
      v.pop_back();
    else {
      scanf("%lld", &a);
      for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
        if (*it == a) {
          v.erase(it);
          break;
        }
      }
    }
  }
  for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
    printf("%d ", *it);
  }
  putchar('\\n');
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  list<long long> l;
  long long n;
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      long long k;
      scanf("%lld", &k);
      l.push_front(k);
    } else {
      if (s.size() == 6) {
        long long k;
        scanf("%lld", &k);
        for (auto i = l.begin(); i != l.end(); i++) {
          if (*i == k) {
            l.erase(i);
            break;
          }
        }
      } else if (s[6] == 'F')
        l.pop_front();
      else
        l.pop_back();
    }
  }
  bool y = false;
  for (auto i = l.begin(); i != l.end(); i++) {
    if (y)
      cout << ' ';
    cout << *i;
    y = true;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <string>
using namespace std;
int main() {
  list<long long> l;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      long long k;
      cin >> k;
      l.push_front(k);
    } else {
      if (s.size() == 6) {
        long long k;
        cin >> k;
        for (auto i = l.begin(); i != l.end(); i++) {
          if (*i == k) {
            l.erase(i);
            break;
          }
        }
      } else if (s[6] == 'F')
        l.pop_front();
      else
        l.pop_back();
    }
  }
  bool y = false;
  for (auto i = l.begin(); i != l.end(); i++) {
    if (y)
      cout << ' ';
    cout << *i;
    y = true;
  }
  cout << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define INF 120000000

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

int main() {
  list<int> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    char tmp[100];
    int a;
    scanf("%s", &tmp);
    string s = tmp;
    if (s != "deleteLast" && s != "deleteFirst") {
      scanf("%d", &a);
    }

    if (s == "insert") {
      l.push_front(a);
    } else if (s == "delete") {
      for (auto it = l.begin(); it != l.end(); it++) {
        if (*it == a) {
          l.erase(it);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define INF 120000000

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

int main() {
  list<int> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    int a;
    cin >> s;
    if (s != "deleteLast" && s != "deleteFirst") {
      cin >> a;
    }

    if (s == "insert") {
      l.push_front(a);
    } else if (s == "delete") {
      for (auto it = l.begin(); it != l.end(); it++) {
        if (*it == a) {
          l.erase(it);
          break;
        }
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    }
  }

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

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <list>

using namespace std;

list<int> myList;

int main() {
  int n, num;
  char command[15];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", command);
    if ('i' == command[0]) {
      scanf("%d", &num);
      myList.push_front(num);
    } else if ('F' == command[6]) {
      myList.pop_front();
    } else if ('L' == command[6]) {
      myList.pop_back();
    } else {
      scanf("%d", &num);
      for (list<int>::iterator iter = myList.begin(); iter != myList.end();
           iter++) {
        if (num == *iter) {
          myList.erase(iter);
          break;
        }
      }
    }
  }
  for (list<int>::iterator iter = myList.begin(); iter != myList.end();
       iter++) {
    if (iter != myList.begin())
      printf(" ");
    printf("%d", (*iter));
  }
  printf("\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <string>

using namespace std;

list<int> myList;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int num;
    string command;
    cin >> command;
    if ('i' == command[0]) {
      cin >> num;
      myList.push_front(num);
    } else if ('F' == command[6]) {
      myList.pop_front();
    } else if ('L' == command[6]) {
      myList.pop_back();
    } else {
      cin >> num;
      for (list<int>::iterator iter = myList.begin(); iter != myList.end();
           iter++) {
        if (num == *iter) {
          myList.erase(iter);
          break;
        }
      }
    }
  }
  for (list<int>::iterator iter = myList.begin(); iter != myList.end();
       iter++) {
    if (iter != myList.begin())
      cout << " ";
    cout << (*iter);
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  main.cpp
//  DoublyLinkedList
//
//  Created by Shimaya Wataru on 2017/05/11.
//  Copyright ?? 2017??´ Wataru Shimaya. All rights reserved.
//

#include <iostream>
using namespace std;

template <class T> class Node {

public:
  T key;
  Node *prev = nullptr;
  Node *next = nullptr;

  Node(T key, Node *prev, Node *next) {
    this->key = key;
    this->prev = prev;
    this->next = next;
  }
};

template <class T> class WLL {
public:
  Node<T> *dummy = nullptr;

  WLL() {
    dummy = new Node<T>(T(), nullptr, nullptr);
    this->dummy->next = dummy;
    this->dummy->prev = dummy;
  }

  void insertKey(T key) {
    Node<T> *newnode = new Node<T>(key, dummy, dummy->next);
    dummy->next->prev = newnode;
    dummy->next = newnode;
  }

  void deleteKey(T key) {
    Node<T> *n = dummy->next;
    while (n != dummy) {
      if (n->key == key) {
        n->prev->next = n->next;
        n->next->prev = n->prev;
        delete n;
        return;
      }
      n = n->next;
    }
  }

  void deleteFirst() {
    Node<T> *newTop = dummy->next->next;
    delete dummy->next;
    newTop->prev = dummy;
    dummy->next = newTop;
  }

  void deleteLast() {
    Node<T> *newlast = dummy->prev->prev;
    delete dummy->prev;
    newlast->next = dummy;
    dummy->prev = newlast;
  }
};

int main(int argc, const char *argv[]) {
  // insert code here...
  WLL<int> wll;

  int N;
  cin >> N;

  string com;
  int val;
  for (int i = 0; i < N; ++i) {
    cin >> com;
    if (com == "insert") {
      cin >> val;
      wll.insertKey(val);
    } else if (com == "delete") {
      cin >> val;
      wll.deleteKey(val);
    } else if (com == "deleteFirst") {
      wll.deleteFirst();
    } else {
      wll.deleteLast();
    }
  }

  Node<int> *n = wll.dummy->next;
  while (n != wll.dummy->prev) {
    cout << n->key << " ";
    n = n->next;
  }
  cout << n->key << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  list<long> dll;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string cmd;
    cin >> cmd;
    if (cmd == "insert") {
      long x;
      cin >> x;
      dll.push_front(x);
    } else if (cmd == "delete") {
      long x;
      cin >> x;
      auto itr = dll.begin();
      while (1) {
        if (*itr == x) {
          dll.erase(itr);
          break;
        } else if (itr == dll.end()) {
          break;
        }
        ++itr;
      }
    } else if (cmd == "deleteFirst") {
      dll.pop_front();
    } else if (cmd == "deleteLast") {
      dll.pop_back();
    }
  }

  while (dll.empty() == false) {
    cout << *(dll.begin());
    dll.pop_front();
    if (dll.empty()) {
      cout << endl;
      break;
    } else {
      cout << " ";
    }
  }
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <list>

using namespace std;

int main() {
  int n, x, siz;
  char com[20];
  list<int> li;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", com);
    if (com[0] == 'i') {
      scanf("%d", &x);
      li.push_front(x);
    } else if (com[6] == 'L')
      li.pop_back();
    else if (com[6] == 'F')
      li.pop_front();
    else if (com[0] == 'd') {
      scanf("%d", &x);
      siz = li.size();
      for (list<int>::iterator it = li.begin(); it != li.end(); ++it) {
        if (*it == x) {
          li.erase(it);
          break;
        }
      }
    }
  }
  int i = 0;
  for (list<int>::iterator it = li.begin(); it != li.end(); ++it) {
    if (i++)
      printf(" ");
    printf("%d", *it);
  }
  printf("\\n");

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>

using namespace std;

int main() {
  int n, x, siz;
  char com[20];
  list<int> li;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", com);
    if (com[0] == 'i') {
      scanf("%d", &x);
      li.push_front(x);
    } else if (com[6] == 'L')
      li.pop_back();
    else if (com[6] == 'F')
      li.pop_front();
    else if (com[0] == 'd') {
      scanf("%d", &x);
      siz = li.size();
      for (int i = 0; i < siz; ++i) {
        if (x == *next(li.begin(), i)) {
          li.erase(next(li.begin(), i));
          break;
        }
      }
    }
  }
  siz = li.size();
  for (int i = 0; i < siz; ++i) {
    if (i)
      printf(" ");
    printf("%d", *next(li.begin(), i));
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
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 *x = nil->next;
  while (x != nil && x->key != key)
    x = x->next;
  return x;
}
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 printList() {
  Node *t = nil->next;
  while (t != nil) {
    printf("%d", t->key);
    if (t->next != nil)
      printf(" ");
    t = t->next;
  }
  printf("\\n");
}

int main() {
  int n, key;
  scanf("%d", &n);
  init();
  while (n--) {
    char s[20];
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else {
      if (s[6] == 'F')
        deleteFirst();
      else if (s[6] == 'L')
        deleteLast();
      else {
        scanf("%d", &key);
        deleteNode(listSearch(key));
      }
    }
    // printList();
  }
  printList();

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
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 *x = nil->next;
  while (x->key != key)
    x = x->next;
  return x;
}
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); }

int main() {
  int n, key;
  scanf("%d", &n);
  init();
  while (n--) {
    char s[20];
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &key);
      insert(key);
    } else {
      if (s[6] == 'F')
        deleteFirst();
      else if (s[6] == 'L')
        deleteLast();
      else {
        scanf("%d", &key);
        deleteNode(listSearch(key));
      }
    }
  }
  Node *t = nil->next;
  while (t != nil) {
    printf("%d", t->key);
    if (t->next != nil)
      printf(" ");
    t = t->next;
  }
  printf("\\n");
  return 0;
}