hi,
j'ai un big probleme, j'ai le code suivant:
#include "list.h"
void list_init(LIST *l, int (*cmp)())
{
l->cmp = cmp;
l->first = (LIST_NODE*)0;
l->len = 0; // empty list, len = 0
}
static LIST_NODE * list_new_node(void *item)
{
LIST_NODE *ln;
if (!(ln = (LIST_NODE*) malloc(sizeof(LIST_NODE)))) {
printf("Unable to allocate more LIST_NODE\n");
exit(1);
}
ln->data = item;
ln->next = (LIST_NODE*)0;
return ln;
}
void *list_search(LIST * l, void *item)
{
LIST_NODE *cur;
if (!l->cmp) return 0; // if no cmp() function, never find item
for(cur = l->first; cur; cur = cur->next) if ((l->cmp)(cur->data, item) == 0) return cur->data; // found
return 0;
}
void list_add_tail(LIST *l, void *item)
{
LIST_NODE *ln, *prev;
if (l->len == 0) { // empty list
list_add_head(l, item);
} else {
ln = list_new_node(item);
// search for end of list
for (prev = l->first; prev->next; prev = prev->next);
prev->next = ln;
l->len++;
}
}
void list_add(LIST *l, void *item)
{
LIST_NODE *cur;
LIST_NODE *prev; // previous
LIST_NODE *ln;
if (!l->cmp) {
list_add_head(l, item); // if no cmp() function, add without sorting
return;
}
// build a new node to store pointer on data
ln = list_new_node(item);
l->len++;
if (!l->first){
//printf("add empty\n");
l->first = ln;
return;
}
if ((l->cmp)(item, l->first->data) < 0) {
//printf("add head\n");
ln->next = l->first;
l->first = ln;
return;
}
prev = l->first;
for (cur = l->first->next; cur; cur = cur->next) {
if ((l->cmp)(prev->data, item) <= 0) {
//printf("after or equal to prev\n");
if ((l->cmp)(item, cur->data) < 0) {
//printf("before cur\n");
//printf("insert\n");
ln->next = cur;
prev->next = ln;
return;
} else {
//printf("after or equal to cur\n");
}
} else {
//printf("before prev\n");
}
//printf("next\n");
prev = cur;
}
//printf("add tail\n");
prev->next = ln;
}
/* end of: list.c */
/*--------------------------------------------------------------
* File: list.h
*
* Purpose: Generic list management library
*
* Author: LESTER-UBS
*
* Date: November 2003
*------------------------------------------------------------*/
#ifndef _H_LIST_H_
#define _H_LIST_H_
// A node in the list
typedef struct list_node {
struct list_node * next; // pointer to next element in list
void * data; // pointer to data
} LIST_NODE;
// A list
typedef struct list {
LIST_NODE * first; // pointer to first node
int (*cmp)(); // pointer to comparison function (like strcmp())
int len; // number of items in the list
} LIST;
extern void list_init(LIST *l, int (*)());
extern void list_add(LIST *, void *);
extern void list_add_head(LIST *, void *);
extern void * list_search(LIST *, void *);
extern int list_len(LIST *);
#endif // _H_LIST_H_
/* end of: list.h */
et mon main
#include "list.h"
#include <stdio.h>
#include <string.h>
int main(){
LIST aList;
int i, a, *j;
char b;
list_init(&aList,0);
for (i=0;i<10;i++){
j = (int*)malloc(sizeof(int));
*j = i;
list_add(&aList, j);
}
a = list_len(&aList);
printf("taille=%d",a);
return 0;
}
C code a ete fais par un gas dont il faut que je reprenne le travail.
Cependant je pîge pas comment il a pu faire pour recuperer un element de ca liste...
En fait j'aimerai pouvoir recuperer la valeur d'un element de la liste en fonction de sa place.
Une sorte de getListe(position)
Generalement je fais du java et la c un peu delicat pour moi...
merci.
a+