#include "stdlib.h"
#include "index_list.h"
#include "dll.h"

struct list_head** index_list_init(int n) {
  struct list_head **r, *head;
  r = calloc(n+1, sizeof(struct list_head*));
  head = malloc(sizeof(struct list_head));
  list_init(head);
  r[0] = head;
  return r;
}

void index_list_add(struct list_head **a, int n, struct list_head *h) {
  a[n] = h;
  list_add_tail(h, a[0]);
}

struct list_head *index_list_remove(struct list_head **a, int n) {
  struct list_head *r;
  r = a[n];
  a[n] = NULL;
  list_del(r);
  return r;
}

void index_list_free(struct list_head **a) {
  struct list_head *head, *itr, *t;
  head = a[0];
  itr = head->next;
  while (itr != head) {
    t = itr;
    itr = itr->next;
    free(t);
  }
  free(head);
  free(a);
}

