Bibliothèque de listes chainées
Bonjour,
j'ai codé une petite bibliothèque pour la gestion des listes chainées et j'aurai voulue savoir si tout marché bien, pas de bug caché ou d'erreur de programmation... Voivi le code:
list.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| typedef struct _list
{
struct _list *prev;
struct _list *next;
void *data;
} list;
list *list_append( list *, void * );
list *list_remove( list *, const void * );
list *list_next( list * );
list *list_prev( list * );
list *list_first( list * );
list *list_last( list * );
void list_free( list * );
size_t list_length( list * ); |
list.c
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| #include <stdio.h>
#include <stdlib.h>
#include "list.h"
list *list_append( list *l, void *data )
{
list *m;
/* --- --- */
m = malloc( sizeof( *m ) );
if( m == NULL )
{
fprintf( stderr, "Memoire insufisante\n" );
list_free( l );
exit( EXIT_FAILURE );
}
m->data = data;
if( l == NULL )
{
m->prev = m->next = NULL;
l = m;
}
else
{
l = list_last( l );
m->prev = l;
m->next = NULL;
l->next = m;
}
/* --- --- */
return( list_first( l ) );
}
list *list_remove( list *l, const void *lost_data )
{
list *p, *n;
/* --- --- */
if( l == NULL )
return( NULL );
p = l->prev;
n = l->next;
lost_data = l->data;
free( l );
p->next = n;
n->prev = p;
/* --- --- */
return( list_first( l ) );
}
list *list_next( list *l )
{
/* --- --- */
if( l == NULL )
return( NULL );
/* --- --- */
return( l->next );
}
list *list_prev( list *l )
{
/* --- --- */
if( l == NULL )
return( NULL );
/* --- --- */
return( l->prev );
}
list *list_first( list *l )
{
/* --- --- */
if( l == NULL )
return( NULL );
while( l->prev != NULL )
l = l->prev;
/* --- --- */
return( l );
}
list *list_last( list *l )
{
/* --- --- */
if( l == NULL )
return( NULL );
while( l->next != NULL )
l = l->next;
/* --- --- */
return( l );
}
void list_free( list *l )
{
list *p;
/* --- --- */
l = list_last( l );
while( l != NULL )
{
p = list_prev( l );
free( l );
l = p;
}
l = NULL;
/* --- --- */
return;
}
size_t list_length( list *l )
{
int n = 0;
/* --- --- */
l = list_first( l );
while( l != NULL )
{
n++;
l = list_next( l );
}
/* --- --- */
return( n );
} |
Merci.
Re: Bibliothèque de listes chainées
Citation:
Envoyé par gege2061
j'ai codé une petite bibliothèque pour la gestion des listes chainées et j'aurai voulue savoir si tout marché bien, pas de bug caché ou d'erreur de programmation... Voici le code:
Ca a l'air pas mal. J'ai pas compris comment fonctionne list_remove(). A quoi sert le 2 eme parametre ?