Skip to content
Snippets Groups Projects
Commit c8169c1a authored by Emilien Blondeau's avatar Emilien Blondeau
Browse files

oui

parent cc6bb2ce
No related branches found
No related tags found
No related merge requests found
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;
}
......@@ -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*);
......
#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));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment