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

#include "list.h"

list new_singleton_list(void *content) {
  list l = (list) malloc(sizeof(list_node));
  l->content = content;
  l->succ = NULL;
  l->prev = NULL;
  return l;
}

void delete_list(list l) {
  if (!l) return;
  list prec, head = l;
  do {
    prec = l;
    l = l->succ;
    free(prec);
  } while (l && l != head);
  //delete l;
  //free(l);
}

void print_list_graph(list l, const char *name) {
  if (!l) printf("%s : . | (empty list !!)", name);
  else {
    list head = l;
    printf("%s : .", name);
    if (!head->prev) printf("->[%u]", *(unsigned *)(head->content));
    else printf("<->[%u]", *(unsigned *)(head->content));
    while (l->succ && l->succ != head) {
      l = l->succ;
      if (!l->prev) printf("->[%u]", *(unsigned *)(l->content));
      else printf("<->[%u]", *(unsigned *)(l->content));
    }
    (!l->succ) ? printf(" |") : printf(" ...");
  }
  printf("\n");
}
