Commit b47579de authored by Lenitra's avatar Lenitra
Browse files

listes finies ?

parent 78b9be6a
Loading
Loading
Loading
Loading
+33 −37
Changes for src/list.c: 33 added lines, 37 removed lines.
Original line number Diff line number Diff line
#include <stdlib.h>

struct cell_t {
struct cell_t
{
  void *val;
  unsigned long int id;
  struct cell_t *next;
@@ -8,15 +9,18 @@ struct cell_t {

typedef struct cell_t *list_t;

list_t list_empty(){
list_t list_empty()
{
  return NULL;
}

int list_is_empty(list_t l){
int list_is_empty(list_t l)
{
  return l == NULL;
}

list_t list_push(list_t l, void *x){
list_t list_push(list_t l, void *x)
{
  list_t cell = malloc(sizeof(*cell));
  cell->val = x;
  if (list_is_empty(l))
@@ -32,59 +36,51 @@ list_t list_push(list_t l, void *x){
  return cell;
}

list_t list_tail(list_t l){
  if(list_is_empty(l)){
list_t list_tail(list_t l)
{
  if (list_is_empty(l))
    return NULL;
  }
  // Get all the list whithout the first element
  list_t l_tail = l->next;
  free(l);
  return l_tail;

  return l->next;
}

void* list_pop(list_t* l){ 
  // remove the first element of l and return it
  if(list_is_empty(*l)){
void *list_pop(list_t *l)
{
  if (list_is_empty(*l))
    return NULL;
  }
  struct cell_t* c = *l;
  *l = c->next;
  void* val = c->val;
  free(c);
  return val;
  list_t tmp = *l;
  *l = tmp->next;
  return tmp->val;
}

void* list_top(list_t l){
  if(list_is_empty(l)){
void *list_top(list_t l)
{
  if (list_is_empty(l))
    return NULL;
  }

  return l->val;
}

void list_destroy(list_t l, void (*free)(void*)){
  // free all the memory allocated by l
  while(!list_is_empty(l)){
void list_destroy(list_t l, void (*free_void)(void *))
{
  while (!list_is_empty(l))
  {
    free(list_pop(&l));
  }
}

// return the found element or NULL
void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
  while(!list_is_empty(l)){
    if(eq(x, l->val)){
      return l->val;
    }
    l = l->next;
  }
  return NULL;
void *list_in(list_t l, void *x, int (*eq)(void *, void *))
{
}

unsigned long int list_len(list_t l){
unsigned long int list_len(list_t l)
{
  unsigned long int len = 0;
  while(!list_is_empty(l)){
  while (!list_is_empty(l))
  {
    len++;
    l = l->next;
  }
  return len;
}