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

#include <cstdio>
#include <cstring>

struct node {
  unsigned int element;
  node *next;
  node *prev;
};

struct list {
  int count;
  node *first;
  node *last;
};

list *list_init(void) {
  list *new_list = new list;
  new_list->count = 0;
  new_list->first = nullptr;
  return new_list;
}

void list_insert(list &my_list, unsigned int element) {

  node *new_node = new node;
  new_node->element = element;
  new_node->prev = nullptr;

  if (my_list.count == 0) {
    new_node->next = nullptr;
    my_list.first = new_node;
    my_list.last = new_node;
  } else {
    new_node->next = my_list.first; // forward link
    my_list.first->prev = new_node; // backward link
    my_list.first = new_node;
  }
  my_list.count++;
}

// for list with only one element
void list_delete_single(list &my_list) {
  node *nodePtr = my_list.first;
  my_list.first = nullptr;
  my_list.last = nullptr;
  my_list.count--;
  delete nodePtr;
}

// delete first element from list
void list_delete_first(list &my_list) {
  if (my_list.count == 1) {
    list_delete_single(my_list);
  } else {
    node *nodePtr = my_list.first; // temporary node pointer
    my_list.first = nodePtr->next; // change head of list
    my_list.first->prev = nullptr; // remove backward link
    delete nodePtr;
    my_list.count--;
  }
}

// delete last element from list
void list_delete_last(list &my_list) {
  if (my_list.count == 1) {
    list_delete_single(my_list);
  } else {
    node *nodePtr = my_list.last;
    my_list.last = nodePtr->prev; // change tail of list
    my_list.last->next = nullptr; // remove forward link
    delete nodePtr;
    my_list.count--;
  }
}

// deleting the first instance of element in list
void list_delete(list &my_list, int element) {

  for (node *nodePtr = my_list.first; nodePtr != nullptr;
       nodePtr = nodePtr->next) {
    if (nodePtr->element == element) {
      if (nodePtr->prev == nullptr) { // if at head of list
        list_delete_first(my_list);
      } else if (nodePtr->next == nullptr) { // if at tail of list
        list_delete_last(my_list);
      } else { // somewhere within list
        nodePtr->prev->next = nodePtr->next;
        nodePtr->next->prev = nodePtr->prev; //"detach" from list
        delete nodePtr;
      }
      break;
    }
  }
}

void list_print(list &my_list) {
  for (node *nodePtr = my_list.first; nodePtr != nullptr;
       nodePtr = nodePtr->next) {
    printf("%u", nodePtr->element);
    if (nodePtr->next == nullptr) { // if end of input
      printf("\\n");                 // print newline
    } else {
      printf(" "); // put a space
    }
  }
}

int main() {
  int n;
  scanf("%d", &n); // number of operations

  list *my_list = list_init();

  for (int i = 0; i < n; ++i) {
    char com[20];
    int key;
    scanf("%s%d", com, &key);
    if (com[0] == 'i') {
      list_insert(*my_list, key);
    } else if (com[0] == 'd') {
      if (strlen(com) > 6) {
        if (com[6] == 'F') {
          list_delete_first(*my_list);
        } else if (com[6] == 'L') {
          list_delete_last(*my_list);
        }
      } else {
        list_delete(*my_list, key);
      }
    }
  }

  list_print(*my_list);
}

Error: TLE

Fixed submission:

#include <cstddef>
#include <iostream>
#include <string>

struct node {
  unsigned int element;
  node *next;
  node *prev;
};

struct list {
  int count;
  node *first;
  node *last;
};

list *list_init(void) {
  list *new_list = new list;
  new_list->count = 0;
  new_list->first = nullptr;
  return new_list;
}

void list_insert(list &my_list, unsigned int element) {

  node *new_node = new node;
  new_node->element = element;
  new_node->prev = nullptr;

  if (my_list.count == 0) {
    new_node->next = nullptr;
    my_list.first = new_node;
    my_list.last = new_node;
  } else {
    new_node->next = my_list.first; // forward link
    my_list.first->prev = new_node; // backward link
    my_list.first = new_node;
  }
  my_list.count++;
}

// for list with only one element
void list_delete_single(list &my_list) {
  node *nodePtr = my_list.first;
  my_list.first = nullptr;
  my_list.last = nullptr;
  my_list.count--;
  delete nodePtr;
}

// delete first element from list
void list_delete_first(list &my_list) {
  if (my_list.count == 1) {
    list_delete_single(my_list);
  } else {
    node *nodePtr = my_list.first; // temporary node pointer
    my_list.first = nodePtr->next; // change head of list
    my_list.first->prev = nullptr; // remove backward link
    delete nodePtr;
    my_list.count--;
  }
}

// delete last element from list
void list_delete_last(list &my_list) {
  if (my_list.count == 1) {
    list_delete_single(my_list);
  } else {
    node *nodePtr = my_list.last;
    my_list.last = nodePtr->prev; // change tail of list
    my_list.last->next = nullptr; // remove forward link
    delete nodePtr;
    my_list.count--;
  }
}

// deleting the first instance of element in list
void list_delete(list &my_list, int element) {

  for (node *nodePtr = my_list.first; nodePtr != nullptr;
       nodePtr = nodePtr->next) {
    if (nodePtr->element == element) {
      if (nodePtr->prev == nullptr) { // if at head of list
        list_delete_first(my_list);
      } else if (nodePtr->next == nullptr) { // if at tail of list
        list_delete_last(my_list);
      } else { // somewhere within list
        nodePtr->prev->next = nodePtr->next;
        nodePtr->next->prev = nodePtr->prev; //"detach" from list
        delete nodePtr;
      }
      break;
    }
  }
}

void list_print(list &my_list) {
  for (node *nodePtr = my_list.first; nodePtr != nullptr;
       nodePtr = nodePtr->next) {
    printf("%u", nodePtr->element);
    if (nodePtr->next == nullptr) { // if end of input
      printf("\\n");                 // print newline
    } else {
      printf(" "); // put a space
    }
  }
}

int main() {
  int n;
  std::cin >> n; // number of operations

  list *my_list = list_init();

  for (int i = 0; i < n; ++i) {
    std::string operation;
    std::cin >> operation;
    if (operation[0] == 'i') {
      unsigned int value;
      std::cin >> value;
      list_insert(*my_list, value);
    } else {
      if (operation[6] == 0) {
        unsigned int value;
        std::cin >> value;
        list_delete(*my_list, value);
      } else if (operation[6] == 'F') {
        list_delete_first(*my_list);
      } else if (operation[6] == 'L') {
        list_delete_last(*my_list);
      }
    }
  }

  list_print(*my_list);
}