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

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

struct LIST {
  int key;
  struct LIST *former;
  struct LIST *next;
};

typedef struct LIST *LIST_POS;

LIST_POS top = NULL;
LIST_POS tail = NULL;

void Insert(int newKey) {
  LIST_POS newEl = (struct LIST *)malloc(sizeof(struct LIST));
  newEl->key = newKey;
  newEl->former = NULL;
  if (top == NULL) {
    top = newEl;
    newEl->next = NULL;
    tail = newEl;
  } else {
    top->former = newEl;
    newEl->next = top;
    top = newEl;
  }
}

void DelFirst() {
  LIST_POS delPos;
  if (top != NULL) {
    delPos = top;
    if (tail != top)
      top->next->former = NULL;
    else
      tail = NULL;
    top = top->next;
    free(delPos);
  }
}

void DelLast() {
  LIST_POS delPos = tail;
  if (tail != NULL) {
    if (top != tail) {
      delPos->former->next = NULL;
      tail = tail->former;
    } else {
      top = NULL;
      tail = NULL;
    }
    free(delPos);
  }
}

void Delete(int delKey) {
  LIST_POS pos = top;
  while (pos != NULL && pos->key != delKey)
    pos = pos->next;
  if (pos == NULL)
    return;
  else if (pos->key == delKey) {
    if (pos == top)
      DelFirst();
    else if (pos->next == NULL)
      DelLast();
    else {
      pos->next->former = pos->former;
      pos->former->next = pos->next;
      free(pos);
    }
  }
}

void OpenKeys() {
  LIST_POS runP;
  for (runP = top; runP != NULL; runP = runP->next) {
    printf("%d", runP->key);
    if (runP->next != NULL)
      printf(" ");
  }
  printf("\\n");
}

int main() {
  int N;
  char s[20];
  int inpKey;

  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%s", s);
    if (!strcmp(s, "deleteFirst"))
      DelFirst();
    else if (!strcmp(s, "deleteLast"))
      DelLast();
    else {
      scanf("%d", &inpKey);
      if (!strcmp(s, "insert"))
        Insert(inpKey);
      else if (!strcmp(s, "delete"))
        Delete(inpKey);
    }
  }

  OpenKeys();

  return 0;
}


Error: TLE

Fixed submission:

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

struct LIST {
  int key;
  struct LIST *former;
  struct LIST *next;
};

typedef struct LIST *LIST_POS;

LIST_POS top = NULL;

void Insert(int newKey) {
  LIST_POS newEl = (struct LIST *)malloc(sizeof(struct LIST));
  newEl->key = newKey;
  newEl->former = NULL;
  if (top == NULL) {
    top = newEl;
    newEl->next = NULL;
  } else {
    top->former = newEl;
    newEl->next = top;
    top = newEl;
  }
}

void DelFirst() {
  LIST_POS delPos;
  if (top != NULL) {
    delPos = top;
    if (top->next != NULL)
      top->next->former = NULL;
    top = top->next;
    free(delPos);
  }
}

void DelLast() {
  LIST_POS delPos = top;
  if (top != NULL) {
    while (delPos->next != NULL) {
      delPos = delPos->next;
    }
    if (delPos->former != NULL)
      delPos->former->next = NULL;
    else
      top = NULL;
    free(delPos);
  }
}

void Delete(int delKey) {
  LIST_POS pos = top;
  while (pos != NULL && pos->key != delKey)
    pos = pos->next;
  if (pos == NULL)
    return;
  else if (pos->key == delKey) {
    if (pos == top)
      DelFirst();
    else if (pos->next == NULL)
      DelLast();
    else {
      pos->next->former = pos->former;
      pos->former->next = pos->next;
      free(pos);
    }
  }
}

void OpenKeys() {
  LIST_POS runP;
  for (runP = top; runP != NULL; runP = runP->next) {
    printf("%d", runP->key);
    if (runP->next != NULL)
      printf(" ");
  }
  printf("\\n");
}

int main() {
  int N;
  char s[20];
  int inpKey;

  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%s", s);
    if (!strcmp(s, "deleteFirst"))
      DelFirst();
    else if (!strcmp(s, "deleteLast"))
      DelLast();
    else {
      scanf("%d", &inpKey);
      if (!strcmp(s, "insert"))
        Insert(inpKey);
      else if (!strcmp(s, "delete"))
        Delete(inpKey);
    }
  }

  OpenKeys();

  return 0;
}