You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
1.5 KiB

#include "list.h"
#include <stdlib.h>
// Ajoute une case à la fin d'une liste
LCell* append(List* list, LData data)
{
LCell* cell = malloc(sizeof(LCell));
cell->data = data;
cell->next = NULL;
if (*list == NULL)
*list = cell;
else
last(*list)->next = cell;
return cell;
}
// Ajoute une liste à la fin d'une liste
void appendList(List* a, List b)
{
if (*a == NULL)
*a = b;
else
last(*a)->next = b;
}
// Retourne un pointeur vers la dernière case d'une liste
LCell* last(List list)
{
if (list == NULL)
return NULL;
LCell* current = list;
while (current->next != NULL)
current = current->next;
return current;
}
// Sélectionne une valeur aléatoire de la liste
LData pickRandom(List list)
{
if (list == NULL)
{
LData tmp = {NULL, 0, 0};
return tmp;
}
int n = rand() % listSize(list);
LCell* current = list;
for (int i=0; i<n; i++)
current = current->next;
return current->data;
}
// Compte les éléments d'une liste
int listSize(List list)
{
if (list == NULL)
return 0;
int size = 0;
LCell* current = list;
while (current != NULL)
{
current = current->next;
size ++;
}
return size;
}
// Supprime une liste
void deleteList(List* list)
{
if ((*list) == NULL)
return;
deleteList(&(*list)->next);
free((*list)->data.move);
free(*list);
*list = NULL;
}