Language: C

#include <stdint.h>
#include <stdlib.h>

#define UTIL_PRINT_ARRAY(arr, size)                                                                                                        \
  do {                                                                                                                                     \
    int32_t _i;                                                                                                                            \
    for (_i = 0; _i < (size); _i++) {                                                                                                      \
      if (_i != 0) printf(" ");                                                                                                            \
      printf("%d", arr[_i]);                                                                                                               \
    }                                                                                                                                      \
    printf("\n");                                                                                                                          \
  } while (0)

struct double_linked_list_tag {
  double_linked_list_t *prev;
  double_linked_list_t *next;
  int32_t data;
};

typedef struct double_linked_list_tag double_linked_list_t;

double_linked_list_t *double_linked_list_create(void)
{
  double_linked_list_t *l = (double_linked_list_t *)malloc(sizeof(double_linked_list_t));

  if (l == NULL) {
    perror("memory is full\n");
    exit(1);
  }

  l->prev = l;
  l->next = l;

  return l;
}

void double_linked_list_destroy(double_linked_list_t *const l)
{
  if (l == NULL) return;

  double_linked_list_t *list = l->next;

  while (l != list) {
    double_linked_list_t *next = list->next;
    free(list);
    list = next;
  }

  free(l);

  return;
}

bool double_linked_list_insert(double_linked_list_t *const l, int32_t data)
{
  if (l == NULL) return false;

  double_linked_list_t *new = (double_linked_list_t *)malloc(sizeof(double_linked_list_t));

  if (new == NULL) {
    perror("memory is full\n");
    exit(1);
  }

  l->next->prev = new;
  new->next = l->next;
  l->next = new;
  new->prev = l;
  new->data = data;

  return true;
}

static bool check_arguments(double_linked_list_t *const l, int32_t buf[], const size_t size)
{
  if (l == NULL) return false;

  if (size < 1) return false;

  if (buf == NULL) return false;

  return true;
}

size_t double_linked_list_to_array(double_linked_list_t *const l, int32_t buf[], const size_t size)
{
  if (!check_arguments(l, buf, size)) {
    return 0;
  }

  size_t count = 0;

  double_linked_list_t *list = l->next;
  while (list != l) {
    buf[count] = list->data;
    count++;
    if (size <= count) break;
    list = list->next;
  }

  return count;
}

bool double_linked_list_delete(double_linked_list_t *const l, int32_t data)
{
  if (l == NULL) return false;

  double_linked_list_t *list = l->next;

  while (list != l) {
    if (list->data == data) {
      double_linked_list_t *next = list->next;
      double_linked_list_t *prev = list->prev;
      next->prev = list->prev;
      prev->next = list->next;
      free(list);
      return true;
    }
    list = list->next;
  }

  return false;
}

bool double_linked_list_delete_first(double_linked_list_t *const l)
{
  if (l == NULL) return false;

  if (l == l->next) return false;

  double_linked_list_t *head = l->next;
  double_linked_list_t *new_head = head->next;

  l->next = new_head;
  new_head->prev = l;

  free(head);

  return true;
}

bool double_linked_list_delete_last(double_linked_list_t *const l)
{
  if (l == NULL) return false;

  if (l == l->prev) return false;

  double_linked_list_t *last = l->prev;
  double_linked_list_t *new_last = last->prev;

  new_last->next = l;
  l->prev = new_last;

  return true;
}

#if !defined(TEST)
#include <stdio.h>
#include <string.h>

#include "util.h"

int32_t main(void)
{
  double_linked_list_t *l = double_linked_list_create();

  int32_t num = 0;
  scanf("%d", &num);

  int32_t i;
  for (i = 0; i < num; i++) {
    char buf[128];
    int32_t val;
    int32_t ret = scanf("%s %d", buf, &val);
    if (ret == 2) {
      switch (buf[0]) {
      case 'i':
        double_linked_list_insert(l, val);
        break;
      case 'd':
        double_linked_list_delete(l, val);
        break;
      }
    } else {
      if (!strcmp(buf, "deleteFirst")) {
        double_linked_list_delete_first(l);
      } else {
        double_linked_list_delete_last(l);
      }
    }
  }

  int32_t result[1024];
  size_t size = double_linked_list_to_array(l, result, UTIL_ARRAY_SIZE(result));
  UTIL_PRINT_ARRAY(result, size);

  double_linked_list_destroy(l);

  return 0;
}
#endif

Compiler output:
main.c:15:3: error: unknown type name 'double_linked_list_t'
   15 |   double_linked_list_t *prev;
      |   ^~~~~~~~~~~~~~~~~~~~
main.c:16:3: error: unknown type name 'double_linked_list_t'
   16 |   double_linked_list_t *next;
      |   ^~~~~~~~~~~~~~~~~~~~
main.c: In function 'double_linked_list_create':
main.c:27:5: error: implicit declaration of function 'perror' [-Wimplicit-function-declaration]
   27 |     perror("memory is full\n");
      |     ^~~~~~
main.c:31:11: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
   31 |   l->prev = l;
      |           ^
main.c:32:11: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
   32 |   l->next = l;
      |           ^
main.c: In function 'double_linked_list_destroy':
main.c:41:32: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
   41 |   double_linked_list_t *list = l->next;
      |                                ^
main.c:44:34: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
   44 |     double_linked_list_t *next = list->next;
      |                                  ^~~~
main.c: At top level:
main.c:54:1: error: unknown type name 'bool'
   54 | bool double_linked_list_insert(double_linked_list_t *const l, int32_t data)
      | ^~~~
main.c:3:1: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
    2 | #include <stdlib.h>
  +++ |+#include <stdbool.h>
    3 | 
main.c: In function 'double_linked_list_insert':
main.c:56:25: error: 'false' undeclared (first use in this function)
   56 |   if (l == NULL) return false;
      |                         ^~~~~
main.c:56:25: note: 'false' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:56:25: note: each undeclared identifier is reported only once for each function it appears in
main.c:65:10: error: request for member 'prev' in something not a structure or union
   65 |   l->next->prev = new;
      |          ^~
main.c:67:11: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
   67 |   l->next = new;
      |           ^
main.c:68:13: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
   68 |   new->prev = l;
      |             ^
main.c:71:10: error: 'true' undeclared (first use in this function)
   71 |   return true;
      |          ^~~~
main.c:71:10: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: At top level:
main.c:74:8: error: unknown type name 'bool'
   74 | static bool check_arguments(double_linked_list_t *const l, int32_t buf[], const size_t size)
      |        ^~~~
main.c:74:8: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: In function 'check_arguments':
main.c:76:25: error: 'false' undeclared (first use in this function)
   76 |   if (l == NULL) return false;
      |                         ^~~~~
main.c:76:25: note: 'false' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:82:10: error: 'true' undeclared (first use in this function)
   82 |   return true;
      |          ^~~~
main.c:82:10: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: In function 'double_linked_list_to_array':
main.c:93:32: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
   93 |   double_linked_list_t *list = l->next;
      |                                ^
main.c:98:10: error: assignment to 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
   98 |     list = list->next;
      |          ^
main.c: At top level:
main.c:104:1: error: unknown type name 'bool'
  104 | bool double_linked_list_delete(double_linked_list_t *const l, int32_t data)
      | ^~~~
main.c:104:1: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: In function 'double_linked_list_delete':
main.c:106:25: error: 'false' undeclared (first use in this function)
  106 |   if (l == NULL) return false;
      |                         ^~~~~
main.c:106:25: note: 'false' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:108:32: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  108 |   double_linked_list_t *list = l->next;
      |                                ^
main.c:112:36: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  112 |       double_linked_list_t *next = list->next;
      |                                    ^~~~
main.c:113:36: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  113 |       double_linked_list_t *prev = list->prev;
      |                                    ^~~~
main.c:117:14: error: 'true' undeclared (first use in this function)
  117 |       return true;
      |              ^~~~
main.c:117:14: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:119:10: error: assignment to 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  119 |     list = list->next;
      |          ^
main.c: At top level:
main.c:125:1: error: unknown type name 'bool'
  125 | bool double_linked_list_delete_first(double_linked_list_t *const l)
      | ^~~~
main.c:125:1: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: In function 'double_linked_list_delete_first':
main.c:127:25: error: 'false' undeclared (first use in this function)
  127 |   if (l == NULL) return false;
      |                         ^~~~~
main.c:127:25: note: 'false' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:129:9: warning: comparison of distinct pointer types lacks a cast [-Wcompare-distinct-pointer-types]
  129 |   if (l == l->next) return false;
      |         ^~
main.c:131:32: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  131 |   double_linked_list_t *head = l->next;
      |                                ^
main.c:132:36: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  132 |   double_linked_list_t *new_head = head->next;
      |                                    ^~~~
main.c:134:11: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
  134 |   l->next = new_head;
      |           ^
main.c:135:18: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
  135 |   new_head->prev = l;
      |                  ^
main.c:139:10: error: 'true' undeclared (first use in this function)
  139 |   return true;
      |          ^~~~
main.c:139:10: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: At top level:
main.c:142:1: error: unknown type name 'bool'
  142 | bool double_linked_list_delete_last(double_linked_list_t *const l)
      | ^~~~
main.c:142:1: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c: In function 'double_linked_list_delete_last':
main.c:144:25: error: 'false' undeclared (first use in this function)
  144 |   if (l == NULL) return false;
      |                         ^~~~~
main.c:144:25: note: 'false' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:146:9: warning: comparison of distinct pointer types lacks a cast [-Wcompare-distinct-pointer-types]
  146 |   if (l == l->prev) return false;
      |         ^~
main.c:148:32: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  148 |   double_linked_list_t *last = l->prev;
      |                                ^
main.c:149:36: error: initialization of 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} from incompatible pointer type 'int *' [-Wincompatible-pointer-types]
  149 |   double_linked_list_t *new_last = last->prev;
      |                                    ^~~~
main.c:151:18: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
  151 |   new_last->next = l;
      |                  ^
main.c:152:11: error: assignment to 'int *' from incompatible pointer type 'double_linked_list_t *' {aka 'struct double_linked_list_tag *'} [-Wincompatible-pointer-types]
  152 |   l->prev = new_last;
      |           ^
main.c:154:10: error: 'true' undeclared (first use in this function)
  154 |   return true;
      |          ^~~~
main.c:154:10: note: 'true' is defined in header