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
| #include <stdlib.h>
#include <stdio.h>
typedef struct s_elem
{
char info;
struct s_elem *next;
} t_noeud;
typedef struct s_list
{
t_noeud *head;
t_noeud *tail;
int size;
} t_ensemble;
void init_list(t_ensemble *l)
{
l->head = 0;
l->tail = 0;
l->size = 0;
}
void add_elem_end(char info, t_ensemble *l)
{
t_noeud *e;
e = malloc(sizeof (t_noeud));
if (e == 0)
exit(-1);
e->next = 0;
e->info = info;
if (l->tail)
l->tail->next = e;
l->tail = e;
l->size++;
if (!l->head)
l->head = e;
}
char get_elem_at_pos(int position, t_ensemble *l)
{
int i;
t_noeud *e;
e = l->head;
for (i = 0; position > 0 && i < position && position < l->size; i++)
e = e->next;
if (!e)
return (0);
return (e->info);
}
void main()
{
t_ensemble list;
init_list(&list);
add_elem_end('a', &list);
add_elem_end('b', &list);
printf("[%c]", get_elem_at_pos(0, &list));
printf("[%c]", get_elem_at_pos(1, &list));
} |