Commit d731cfb8 authored by Matthieu Dien's avatar Matthieu Dien
Browse files

correction TP1

parent 6fcaad20
Loading
Loading
Loading
Loading

src/bst.c

deleted100644 → 0
+0 −33
Original line number Diff line number Diff line
struct node_t {
  unsigned long int key;
  void* val;
  struct node_t* left;
  struct node_t* right;
};

typedef struct bst_t {
  // Use this to compute a key from a val
  // It is not really a hash function, the key must be unique
  unsigned long int (*h)(void*);

  unsigned long int size;
  struct node_t* tree;
} bst_t;

bst_t bst_empty(void){
}

int bst_is_empty(bst_t t){
}

bst_t bst_add(bst_t t, void* x){
}

void bst_destroy(bst_t t, void (*free_void)(void*)){
}

int bst_in(bst_t t, void* x){
}

unsigned long int bst_size(bst_t t){
}

src/bst.h

deleted100644 → 0
+0 −24
Original line number Diff line number Diff line
#ifndef __BST__
#define __BST__

struct node_t {
  unsigned long int key;
  void* val;
  struct node_t* left;
  struct node_t* right;
};

typedef struct bst_t {
  unsigned long int size;
  struct node_t* tree;
} bst_t;

unsigned long int hash(void*);
bst_t bst_empty(void);
int bst_is_empty(bst_t);
bst_t bst_add(bst_t, void*);
void bst_destroy(bst_t, void (*free_void)(void*));
int bst_in(bst_t, void*);
unsigned long int bst_size(bst_t);

#endif
+28 −1
Original line number Diff line number Diff line
#include <stdlib.h>

struct cell_t {
  void* val;
  unsigned long int id;
@@ -7,30 +9,55 @@ struct cell_t {
typedef struct cell_t* list_t;

list_t list_empty(){
  return NULL;
}

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

list_t list_push(list_t l, void* x){
  list_t res = malloc(sizeof(struct cell_t));
  res->next = l;
  res->val = x;
  res->id = list_is_empty(l) ? 1 : 1 + l->id;
  return res;
}

list_t list_tail(list_t l){
  return list_is_empty(l) ? NULL : l->next;
}

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

void* list_top(list_t l){
  return list_is_empty(l) ? NULL : l->val;
}

void list_destroy(list_t l, void (*free)(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;
    else
      l = l->next;
  return NULL;
}

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