Commit c8169c1a authored by Emilien Blondeau's avatar Emilien Blondeau
Browse files

oui

parent cc6bb2ce
Loading
Loading
Loading
Loading
+58 −8
Changes for src/list.c: 58 added lines, 8 removed lines.
Original line number Diff line number Diff line
struct cell_t {
  void* val;
  unsigned long int id;
  struct cell_t* next;
};

typedef struct cell_t* list_t;
#include <stdlib.h>
#include <stdio.h>
#include "list.h"

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

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

list_t list_push(list_t l, void* x){
  list_t res=malloc(sizeof(struct cell_t));

  if(list_is_empty(l)==1){
    res->val=x;
    res->next=NULL;
    res->id=1;
    return res;
  }

  res->val=x;
  res->next=l;
  res->id=l->id+1;
  return res;
}

list_t list_tail(list_t l){
  if(list_is_empty(l)==0){
    return l->next;
  }
  return NULL;
}

void* list_pop(list_t* l){
  if(list_is_empty(l)==0){
    list_t tmp = (*l);
    (*l)=(*l)->next;
    void* res= tmp->val;
    free(tmp);
    return res;
  }
  return NULL;
}

void* list_top(list_t l){
  if(list_is_empty(l)==0){
    return l->val;
  }
  return NULL;
}

void list_destroy(list_t l, void (*free)(void*)){
void list_destroy(list_t l, void (*free_void)(void*)){
  while(list_is_empty(l)==0){
    free_void(l->val);
    list_t tmp=l;
    l=l->next;
    free(tmp);
  }
}

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

unsigned long int list_len(list_t l){
  if(list_is_empty(l)==0){
    return l->id;
  }

  return 0;
}
+1 −0
Changes for src/list.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ struct cell_t {

typedef struct cell_t* list_t;


list_t list_empty();
int list_is_empty(list_t);
list_t list_push(list_t, void*);
+2 −2
Changes for tests/list_tests.c: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
#include <assert.h>
#include <stdio.h>
#include "linked_list.h"
#include "list.h"
#include "bignum.h"

void test_emptyness() {
@@ -29,7 +29,7 @@ void test_push(){
  l = list_push(l, y);
  l = list_push(l, x);
  l = list_push(l, z);
  
  printf("Oui\n");
  assert(eq(list_top(l), z));
  assert(eq(list_top(list_tail(l)), x));
  assert(eq(list_top(list_tail(list_tail(l))), y));