Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

struct Node *nil;

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

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

void printList() {
  Node *cur = nil->next;
  int isf = 0;

  while (1) {
    if (cur == nil)
      break;
    if (isf++ > 0)
      printf(" ");
    printf("%llu", 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) {
  struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
  new_node->key = key;
  new_node->next = nil->next;
  nil->next->prev = new_node;
  nil->next = new_node;
  new_node->prev = nil;
}

// void insert(unsigned long long int  n) {
//    new_node = (struct Node * )malloc(sizeof(struct Node));
//    new_node->n = n;
//
//    if (!head) {
//        head = new_node;
//        head->next = nil;
//        tail->next = head;
//
//    }
//
// }
//
//
//
//
// void show() {
//     struct Node *node;
//     node = head;
//
//     if(node == tail){
//         cout << node->n << endl;
//     }
//     else{
//         while (node != tail) {
//
//             //cout << tail->n << " prev: " << tail->prev->n << "next: " <<
//             tail->next->n; cout << node->n << " "; node = node->next;
//         }
//         cout << node->n << endl;
//     }
// }
//
// void del(int n) {
//
//     struct Node *node, *prev, *target_node, *next, *tmp, *tmp_tail;
//     node = head;
//     while (node!=tail){
//         if (node->n == n) {
//             break;
//         }
//         node = node->next;
//         //cout << node->n;
//
//     }
//     //cout << "del " << node->n << "prev" << node->prev->n << "next" <<
//     node->next->n <<endl;
//
//     if (head == node){
//         tmp = head;
//         tmp_tail = tail;
//         head = head->next;
//         head->prev = tail;
//         tail->next = head;
//     }
//
//     else if (tail == node) {
//         //cout << "b";
//         tmp = head;
//         tmp_tail = tail;
//         tail = tail->prev;
//         tail->next = head;
//         head->prev = tail;
//     }
//
//
//     else {
//         //cout << "c";
//         tmp = head;
//         prev = node->prev;
//         next = node->next;
//         prev->next = next;
//         next->prev = prev;
//         head = tmp;
//
//     }
//
//
//
// }
//
// void delf(){
//
//     struct Node *node = head, *tmp;
//     tmp = head;
//     head = head->next;
//     head->prev = tail;
//
//     tail->next = head;
//     tmp = tail->next;
//
// }
//
//
// void dell(){
//     struct Node *prev, *tmp;
//     tmp = tail;
//     tail = tail->prev;
//     tail->prev->next = tail;
//     tail->next = head;
//     //tail->prev = tail->prev->prev;
//     cout << "PREV " << tail->n << "prev " << tail->prev->prev->n << " next "
//     << tail->next->n <<endl;
//     //tail = tail->prev;
//     //tail->prev->next->next = tail;
//     //tail->next = head;
//     //tail->prev->next = tail->prev;
//     //tail->next = head;
// }

int main() {
  int n;
  char s[100];
  unsigned long long int x;
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s%llu", s, &x);
    if (s[0] == 'i') {
      insert(x);
    } else if (s[0] == 'd') {
      if (strlen(s) == 6)
        deleteKey(x);
      if (strlen(s) == 11)
        deleteFirst();
      if (strlen(s) == 10)
        deleteLast();
    }
  }
  printList();
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

struct Node {
  unsigned long long int n;
  struct Node *next;
  struct Node *prev;
  bool isTail;
  bool isHead;
};

struct Node *head;
struct Node *new_node;
struct Node *old_head;
struct Node *tail;

// void insert(struct Node node) {
void insert(unsigned long long int n) {
  struct Node *node, *next, *tmp, *prev, *tmp_tail;
  new_node = (struct Node *)malloc(sizeof(struct Node));
  new_node->n = n;
  if (head == NULL && tail == NULL) {
    new_node->prev = new_node;
    new_node->next = new_node;
    head = new_node;
    tail = new_node;
    head->prev = new_node;
    head->next = new_node;
    tail->prev = new_node;
    tail->next = new_node;
    // tail->isTail = true;
  } else {

    if (head->next == tail) {

      if (tail->prev == head) {
        tmp = head;
        tail = tmp;

        head = new_node;
        head->next = tmp;
        head->prev = tail;
        tail->prev = tmp;
      } else {
        tmp = head;
        head = new_node;
        head->next = tmp;
        tail->next = head;
        tail->prev = tmp;
      }
    }

    else {

      tmp = head;
      head = new_node;
      tmp->prev = head;
      head->next = tmp;
      head->prev = tail;
      tail->next = new_node;
    }
  }
}

void init() {
  head = NULL;
  tail = NULL;
}

void show() {
  struct Node *node;
  node = head;

  if (node == tail) {
    cout << node->n << endl;
  } else {
    while (node != tail) {
      cout << node->n << " ";
      node = node->next;
    }
    cout << node->n << endl;
  }
}

void del(int n) {

  struct Node *node, *prev, *target_node, *next, *tmp, *tmp_tail;
  node = head;
  while (node != tail) {
    if (node->n == n) {
      break;
    }
    node = node->next;
    // cout << node->n;
  }
  // cout << "del " << node->n << "prev" << node->prev->n << "next" <<
  // node->next->n <<endl;

  if (head == node) {
    tmp = head;
    tmp_tail = tail;
    head = head->next;
    head->prev = tail;
    tail->next = head;
  }

  else if (tail == node) {
    // cout << "b";
    tmp = head;
    tmp_tail = tail;
    tail = tail->prev;
    tail->next = head;
    head->prev = tail;
  }

  else {
    // cout << "c";
    tmp = head;
    prev = node->prev;
    next = node->next;
    prev->next = next;
    next->prev = prev;
    head = tmp;
  }
}

void delf() {

  struct Node *node = head, *tmp;
  tmp = head;
  head = head->next;
  head->prev = tail;

  tail->next = head;
  tmp = tail->next;
}

void dell() {
  struct Node *prev, *tmp;
  // cout << "tail" << tail->n <<endl;
  if (head != tail) {
    // cout << "d";
    tmp = tail;
    tail = tmp->prev;
    tail->next = tmp->next;

    // tail->prev->next = head;
    // head->prev = tail->prev;
    // tail = head->prev;
    // head->next = head;
    // head->prev = head;

  } else {
    head = tail->prev;
    head->prev = head;
    tail->next = head;
    tail->prev = head;
  }
}

int main() {
  int n;
  char s[100];
  unsigned long long int x;
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s%llu", s, &x);
    if (s[0] == 'i') {
      insert(x);
    } else if (s[0] == 'd') {
      if (strlen(s) == 6)
        del(x);
      if (strlen(s) == 11)
        delf();
      if (strlen(s) == 10)
        dell();
    }
  }
  show();

  return 0;
}