List.c

De wiki-prog
Révision de 24 octobre 2016 à 16:38 par Slashvar (discuter | contributions) (Implementation)

(diff) ← Version précédente | Voir la version courante (diff) | Version suivante → (diff)
Aller à : navigation, rechercher

Header

/* Standard list implementation in C */
 
/* lists.h */
 
# ifndef EPITA_IP_SAMPLES_LISTS_H_
# define EPITA_IP_SAMPLES_LISTS_H_
 
/* simply linked list of integer */
 
struct list
{
  struct list *next;
  int          data;
};
 
static inline
struct list* list_empty(void)
{
  return NULL;
}
 
static inline
int list_is_empty(struct list *list)
{
  return l == NULL;
}
 
struct list* list_push_front(struct list *list, int x);
 
void list_release(struct list *list);
 
# endif /* EPITA_IP_SAMPLES_LISTS_H_ */

Implementation

/* Standard list implementation in C */
 
/* list.c */
 
# include <stdlib.h>
 
# include "lists.h"
 
struct list* list_push_front(struct list *list, int x)
{
  struct list *tmp = malloc(sizeof (struct list));
  if (!tmp)
    return NULL;
  tmp->next = list;
  tmp->data = x;
  return tmp;
}
 
void list_release(struct list *list)
{
  struct list *tmp = list;
  while (list) {
    tmp = list->next;
    free(list);
    list = tmp;
  }
}