#include "util.h"
#include "lists.h"

LIST list_delete_one(l) LIST l; {
  LIST n;
  if (list_is_empty(l)) return NULL_LIST;
  n = list_next(l);
  FREE (l);
  return n;
}

list_delete_all(l) LIST l; {
  LIST n;
  while (! list_is_empty(l)) {
    n = list_next(l);
    FREE (l);
    l = n;
  }
}

LIST list_prepend(l,d) LIST l; UINT d; {
  LIST n = (LIST) malloc (sizeof (*n));
  n -> next = l;
  n -> data = d;
  return n;
}

UINT list_for_all(l,f,s) LIST l;  UINT (*f)(); UINT s; {
  LIST n;
  while (! list_is_empty(l)) {
    s = (*f)(list_data(l), s);
    l = list_next(l);
  }
  return s;
}

LIST list_reverse(l) LIST l; {
  LIST n,o;
  if (list_is_empty(l)) return l;
  o = l;
  l = list_next(l);
  list_next(o) = NULL_LIST;

  while (! list_is_empty(l)) {
    n = list_next(l);
    list_next(l) = o;
    o = l;
    l = n;
  }
  return o;
}
